ASP.NET在访问Controller的方法带参数时怎样防止黑客攻击
生活随笔
收集整理的這篇文章主要介紹了
ASP.NET在访问Controller的方法带参数时怎样防止黑客攻击
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
場(chǎng)景
ASP.NET中MVC添加Controller以及訪(fǎng)問(wèn)其Action:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/106796402
在上面訪(fǎng)問(wèn)Controller的方法時(shí)如果要給方法傳遞一個(gè)參數(shù)可以這樣寫(xiě)
namespace HelloMVC.Controllers {public class HelloController : Controller{// GET: Hellopublic string index(){return "公眾號(hào):霸道的程序猿,推動(dòng)編程相關(guān)教程";}public string Welcome(string name){return "welcome"+name;}} }比如這里給Welcome方法傳遞一個(gè)name參數(shù),那么可以通過(guò)url中
http://localhost:6388/Hello/Welcome?name=badao
進(jìn)行傳遞
?
但是這樣可能會(huì)別黑客傳遞一些可執(zhí)行的腳本代碼等,所以可以通過(guò)以下方法對(duì)參數(shù)進(jìn)行編碼
namespace HelloMVC.Controllers {public class HelloController : Controller{// GET: Hellopublic string index(){return "公眾號(hào):霸道的程序猿,推動(dòng)編程相關(guān)教程";}public string Welcome(string name){return "welcome" + HttpUtility.HtmlEncode(name);}} }這樣在接收參數(shù)時(shí)顯示還是跟上面一樣,但是傳遞的參數(shù)已經(jīng)被重新編碼。
也可以采用如下方式,效果是一樣的。
namespace HelloMVC.Controllers {public class HelloController : Controller{// GET: Hellopublic string index(){return "公眾號(hào):霸道的程序猿,推動(dòng)編程相關(guān)教程";}public string Welcome(string name){return "welcome" + Server.HtmlEncode(name);}} }?
總結(jié)
以上是生活随笔為你收集整理的ASP.NET在访问Controller的方法带参数时怎样防止黑客攻击的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ASP.NET项目在VS中F5与Ctrl
- 下一篇: ASP.NET中添加View与Razor