mvc 與 前端的資料互動安全機制

2021-09-19 20:19:45 字數 4110 閱讀 8865

通過一些**過濾驗證,與前端使用互動token實現限制訪問端,避免使用非正常工具訪問,應可滿足一些等保需求。

控制器**:

/// /// 互動token

///

public queuechangetoken set }

/// /// 訪問次數

///

public int vnums

/// /// 最後訪問時間

///

public datetime lasttime

/// /// 禁止訪問

///

public bool disv

/// /// 最後訪問的路徑

///

public string lasturl

public vobj(string token, string lasttoken, datetime lasttime, string lasturl)

}protected jsonresult msg(ajaxstatu status, object data = null, bool sendtoken = true)

}catch (exception e)

return json(hs);

}protected jsonresult actfunc(action func, bool chktoken = true)

catch (exception e)

}try

catch (exception e)

}protected jsonresult actfunc(funcfunc, bool chktoken = true)

catch (exception e)

}try

catch (exception e)

}/// /// 獲取互動token

///

///

private string getchangetoken()

string token = session["stutoken"].tostring();

//未找到使用者token

if (!stutokendic.containskey(token))

//互動token為空

if (stutokendic[token] == null)

//一定時間段內操作超過一定次數

var last = stutokendic[token];

if (last.disv)

timespan ts = datetime.now - last.lasttime;

int secs = 5;//訪問間隔 5 秒

int nums = 5;//訪問次數 5

if (request.url.absolutepath == last.lasturl)

else

}else

}else

last.lasturl = request.url.absolutepath;

string lasttoken = dataall.dal.common.createtoken();

last.changetoken.enqueue(lasttoken);

while (last.changetoken.count > 20)

stutokendic[token] = last;

return lasttoken;

}/// /// 檢查當前使用者的互動token是否一致

///

///

private void chkchangetoken()

if (!last.changetoken.contains(changetoken))

}#endregion

/// /// 獲取當前登入使用者的token

///

/// 此方法不限制訪問次數

///

//[allowanonymous]

public jsonresult getcurrent(string sid)

return new ;

}else;}

else

}else

}else

}}, false);

}/// /// 獲取使用者許可權選單樹

///

///

public jsonresult getpertree());}

/// /// 退出登入

///

///

public void logout()

}}mvc中的過濾器,加入對stun的免過濾判斷

public class filterconfig

}public class loginattribute : authorizeattribute

var actionanonymous = filtercontext.actiondescriptor.getcustomattributes(typeof(allowanonymousattribute), true) as ienumerable;

if (actionanonymous != null && actionanonymous.any())

/*** 如果請求的區域包含area並且area的名稱等於admin_areas

* 那麼就進行許可權驗證

* */

if (filtercontext.routedata.datatokens.keys.contains("area"))

else

#endregion

return;

}if (!model_sys_users.islogin())

else

}else

}if (!allow)}}

}}

//設定滑動時間

model_sys_users.setnewtime();

//if (model_users.loginname == "admin")

// return;

/*** action方法本身及它所屬控制器都沒有定義skip特性

* 那麼就可以進行許可權驗證

* */

if (!filtercontext.actiondescriptor.attributeexists(false)

&& !filtercontext.actiondescriptor.controllerdescriptor.isdefined(typeof(common.attributes.skipattribute), false))}}

}}

#endregion

}

mounted() );

tools.post("/admin_areas/stun/getpertree", null, function(res) );

},function()

);});

// this.axios.post("/admin_areas/res/gettitle").then(response => );

},

tools.vue中封裝cookie和post方法,post方法中給引數加入ctoken值,並用chkresult來接收返回值取出ctoken寫入cookie

這個方案還有弊端,並不完善,慎用

web前端與後台資料互動

1.前端請求資料url由誰來寫?在開發中,url主要是由後台來寫的,寫好了給前端開發者.如果後台在查詢資料,需要借助查詢條件才能查詢到前端需要的資料時,這時後台會要求前端提供相關的查詢引數,這裡的查詢引數也就是url請求的引數。2.介面文件主要由誰來寫?介面文件也是主要由後台開發者來寫的,因為直接跟...

web前端與後台資料互動

1.前端請求資料url由誰來寫?在開發中,url主要是由後台來寫的,寫好了給前端開發者.如果後台在查詢資料,需要借助查詢條件才能查詢到前端需要的資料時,這時後台會要求前端提供相關的查詢引數,這裡的查詢引數也就是url請求的引數。2.介面文件主要由誰來寫?介面文件也是主要由後台開發者來寫的,因為直接跟...

如何實現前端與後端資料互動

定義變數 var pay 舉例 var ispay false var lastpay 包年包月 設定 pay是指找到id為pay的要素,當對其執行 click 操作時就執行下面函式內容。pay on click button function console.log this attr id pa...