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