這篇文章給大家介紹如何在ASP.NET中防止SQL注入,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
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("您提交的數據有惡意字符!");
Response.End();
}
}
/// <summary>
/// 獲取QueryString中的數據
/// </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;
}//如果檢測存在漏洞
}
return result;
}
public static string []strs = new string[] {"select","drop","exists","exec","insert","delete","update","and","or","user" };//此處我隨便加了幾個,大家可以多加點哈。
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;
}關于如何在ASP.NET中防止SQL注入就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。