asp.net 防注入

来源:互联网 发布:淘宝卖假货 编辑:程序博客网 时间:2024/04/30 21:51

一.如果参数全为数字:
// 检查字符串是否全为数字

public static bool IsNum(string Str)

{
    bool blResult = true;
    if (Str == "")
        blResult = false;
    else
    {
        foreach (char Char in Str)
        {
            if (!Char.IsNumber(Char))
            {
                blResult = false;
                break;
            }
        }
        if (blResult)
            if (int.Parse(Str) == 0)
                blResult = false;
    }
    return blResult;

}

 

应用:
string Topicid = Request.QueryString["Topicid"];

if (!IsNum(Topicid))
    Server.Transfer("Error.aspx?ErrID=404");





二.如果参数为文本.





// Html转换

public static string htmlstr(string chr)

{
    if(chr==null)
        return "";
    chr=chr.Replace("<","<");
    chr=chr.Replace(">",">");
    chr=chr.Replace("/n","<br>");
    chr=chr.Replace("/"",""");
    chr=chr.Replace("'","'");
    chr=chr.Replace(" "," ");
    chr=chr.Replace("/r","");
    return(chr); 

}
应用:string strClass = htmlstr(Request.QueryString["ClassName"]);