C#驗證用戶輸出信息能否包括風險字符串的辦法。本站提示廣大學習愛好者:(C#驗證用戶輸出信息能否包括風險字符串的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#驗證用戶輸出信息能否包括風險字符串的辦法正文
本文實例講述了C#驗證用戶輸出信息能否包括風險字符串的辦法。分享給年夜家供年夜家參考。詳細剖析以下:
這個C#函數可以用於表單輸出數據的後端驗證,斷定用戶能否提交了一些sql相干的風險注入字符
/// <summary>
/// 檢測客戶輸出的字符串能否有用,並將原始字符串修正為有用字符串或空字符串
/// 當檢測到客戶的輸出中有進擊性風險字符串,則前往false,有用前往true。
/// </summary>
/// <param name="input">要檢測的字符串</param>
public static bool IsValidInput(ref string input)
{
try
{
if (IsNullOrEmpty(input))
{
//假如是空值,則跳出
return true;
}
else
{
//調換單引號
input = input.WordStr("'", "''").Trim();
//檢測進擊性風險字符串
string testString = "and |or |exec |insert |select |delete |update |count |chr |mid |master |truncate |char |declare ";
string[] testArray = testString.Split('|');
foreach (string testStr in testArray)
{
if (input.ToLower().IndexOf(testStr) != -1)
{
//檢測到進擊字符串,清空傳入的值
input = "";
return false;
}
}
//未檢測到進擊字符串
return true;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
願望本文所述對年夜家的C#法式設計有所贊助。