本文實(shí)例講述了ASP.NET防止SQL注入的方法。分享給大家供大家參考,具體如下:
最近接手別人一個(gè)項(xiàng)目,發(fā)現(xiàn)存在SQL注入漏洞,因?yàn)椴幌敫奶啻a,所以那種參數(shù)法防注入呢我就用不著了。只能用傳統(tǒng)的笨一點(diǎn)的辦法了。
1、新建Global.asax文件。
2、加入如下代碼:
void Application_BeginRequest(object sender, EventArgs e) { bool result = false; if (Request.RequestType.ToUpper() == "POST") { //post方式的我就不寫了。 } else { result = ValidUrlGetData(); } if (result) { Response.Write("您提交的數(shù)據(jù)有惡意字符!"); Response.End(); } } /// <summary> /// 獲取QueryString中的數(shù)據(jù) /// </summary> public static bool ValidUrlGetData() { bool result = false; for (int i = 0; i < HttpContext.Current.Request.QueryString.Count; i++) { result = Validate(HttpContext.Current.Request.QueryString[i].ToString()); if (result) { break; }//如果檢測(cè)存在漏洞 } return result; } public static string []strs = new string[] {"select","drop","exists","exec","insert","delete","update","and","or","user" };//此處我隨便加了幾個(gè),大家可以多加點(diǎn)哈。 public static bool Validate(string str) { for (int i = 0; i < strs.Length; i++) { if (str.IndexOf(strs[i]) != -1) { return true; break; } } return false; }
更多關(guān)于asp.net相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《asp.net優(yōu)化技巧總結(jié)》、《asp.net字符串操作技巧匯總》、《asp.net操作XML技巧總結(jié)》、《asp.net文件操作技巧匯總》、《asp.net ajax技巧總結(jié)專題》及《asp.net緩存操作技巧總結(jié)》。
希望本文所述對(duì)大家asp.net程序設(shè)計(jì)有所幫助。
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com