1. 程式人生 > 實用技巧 >C#-防止使用者輸入具有風險的敏感字元

C#-防止使用者輸入具有風險的敏感字元

  最近有涉及到要防止使用者在網頁文字框中輸入具有風險的敏感字元所以特地編寫了一套針對使用者輸入的字元進行安全過濾的一個方法,在後臺接收到使用者輸入的字元後呼叫執行該方法即可完成過濾操作,主要使用正則來匹配並替換掉敏感字元!

    /// <summary>
    /// obj向string轉換,替換具有風險的敏感字元並去除多餘的空格;
    /// </summary>
    /// <param name="o"></param>
    /// <returns></returns>
    public static string RequestFilter(this
object o) { if (o == DBNull.Value || o == null) { return ""; } else { string str = o.ToString().Trim(); //刪除指令碼 str = Regex.Replace(str, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
//刪除HTML str = Regex.Replace(str, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase); str = Regex.Replace(str, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase); str = Regex.Replace(str, @"-->", "", RegexOptions.IgnoreCase); str = Regex.Replace(str, @"
<!--.*", "", RegexOptions.IgnoreCase); str = Regex.Replace(str, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase); str = Regex.Replace(str, @"&(amp|#38);", "&", RegexOptions.IgnoreCase); str = Regex.Replace(str, @"&(lt|#60);", "<", RegexOptions.IgnoreCase); str = Regex.Replace(str, @"&(gt|#62);", ">", RegexOptions.IgnoreCase); str = Regex.Replace(str, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase); str = Regex.Replace(str, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase); str = Regex.Replace(str, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase); str = Regex.Replace(str, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase); str = Regex.Replace(str, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase); str = Regex.Replace(str, @"&#(\d+);", "", RegexOptions.IgnoreCase); str = Regex.Replace(str, "xp_cmdshell", "", RegexOptions.IgnoreCase); //刪除與資料庫相關的詞 str = Regex.Replace(str, "select", "", RegexOptions.IgnoreCase); str = Regex.Replace(str, "insert", "", RegexOptions.IgnoreCase); str = Regex.Replace(str, "delete from", "", RegexOptions.IgnoreCase); str = Regex.Replace(str, "count''", "", RegexOptions.IgnoreCase); str = Regex.Replace(str, "drop table", "", RegexOptions.IgnoreCase); str = Regex.Replace(str, "truncate", "", RegexOptions.IgnoreCase); str = Regex.Replace(str, "asc", "", RegexOptions.IgnoreCase); str = Regex.Replace(str, "mid", "", RegexOptions.IgnoreCase); str = Regex.Replace(str, "char", "", RegexOptions.IgnoreCase); str = Regex.Replace(str, "xp_cmdshell", "", RegexOptions.IgnoreCase); str = Regex.Replace(str, "exec master", "", RegexOptions.IgnoreCase); str = Regex.Replace(str, "net localgroup administrators", "", RegexOptions.IgnoreCase); str = Regex.Replace(str, "and", "", RegexOptions.IgnoreCase); str = Regex.Replace(str, "net user", "", RegexOptions.IgnoreCase); str = Regex.Replace(str, "or", "", RegexOptions.IgnoreCase); str = Regex.Replace(str, "net", "", RegexOptions.IgnoreCase); str = Regex.Replace(str, "delete", "", RegexOptions.IgnoreCase); str = Regex.Replace(str, "drop", "", RegexOptions.IgnoreCase); str = Regex.Replace(str, "script", "", RegexOptions.IgnoreCase); str = str.Replace("<", ""); str = str.Replace(">", ""); str = str.Replace("*", ""); str = str.Replace("--", ""); str = str.Replace("?", ""); str = str.Replace(",", ""); str = str.Replace("/", ""); str = str.Replace(";", ""); str = str.Replace("*/", ""); str = str.Replace("\r\n", ""); return str; } }