QueryString 整站过滤

来源:互联网 发布:淘宝详情页素材网站 编辑:程序博客网 时间:2024/04/30 21:13

对于QueryString的传值是每个程序员都要用到的。但是如何过滤QueryString的方法万千。下面我整理一个方法
大家继续优化讨论

第一步 web.config 加入键值

例如:
<add key="SafeRequest" value="id-int32,nid-int32,xid-int32,keyword-char" /> 

第二步 Global.asax 加入如下

void Application_BeginRequest(object source, EventArgs e)
    {
        String[] SafeRequest = System.Web.Configuration.WebConfigurationManager.AppSettings["SafeRequest"].ToString().Split(',');
        for (int i = 0; i < SafeRequest.Length; i++)
        {
            String RequestName = SafeRequest[i].Split('-')[0];
            String RequestType = SafeRequest[i].Split('-')[1];
            isValidRequest(RequestName, RequestType);
        }
       
    }
    public void isValidRequest(string parameterName, string parameterType)
    {
        string parameterValue = Request.QueryString[parameterName];
        if(parameterValue == null) return;

        if(parameterType.Equals("int32"))
        {
//future.mystring.IsNumeric是我自己做的一个类判断是不是数字。大家根据自己需要可以自行写一段(Bool)
            if(!future.mystring.IsNumeric(parameterValue)) Response.Redirect("index.aspx");
        }
        if (parameterType.Equals("char"))
        {
////future.mystring.IsNumeric是我自己做的一个类判断是不是字符,是否含有敏感字符。大家根据自己需要可以自行写一段(Bool)
            if (!future.mystring.MatchSqlString(parameterValue)) Response.Redirect("index.aspx");
        }

    } 

 这样整站通用了只要是地址栏传值都会经过过滤的 如果不合法 都会跳转到自定义页面!!本例提供基本思路和代码希望大家继续讨论优化。。。。。。。