您的位置:首页 > 其它

防止用户恶意输入信息

2012-11-16 13:02 204 查看
以下是函数具体说明,可以直接引用。
/// <summary>
/// 此方法用于确认用户输入的不是恶意信息
/// </summary>
/// <param name="text">用户输入信息</param>
/// <param name="maxLength">输入的最大长度</param>
public static string InputText(string text, int maxLength)
{
text = text.Trim();
if (string.IsNullOrEmpty(text))
return string.Empty;
if (text.Length > maxLength)
text = text.Substring(0, maxLength);
//将网页中非法和有攻击性的符号替换掉,以防sql注入!返回正常数据
text = Regex.Replace(text, "[\\s]{2,}", " "); // 2个或以上的空格
text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //<br> html换行符
text = Regex.Replace(text, "([b%7cb][s%7cs][p%7cp];//s]\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); //  html空格符
text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); // 任何其他的标签
text = text.Replace("'", "''");// 单引号
return text;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: