• Net程序如何防止被注入(整站通用)


    防止sql注入,通常一个一个文件修改不仅麻烦而且还有漏掉的危险,下面我说一上如何从整个系统防止注入。

    做到以下三步,相信的程序将会比较安全了,而且对整个网站的维护也将会变的简单。

    一、数据验证类:
    parameterCheck.cs

     
    public class parameterCheck{
        public static bool isEmail(string emailString){
            return System.Text.RegularExpressions.Regex.IsMatch(emailString, "['\\w_-]+(\\.['\\w_-]+)*@['\\w_-]+(\\.['\\w_-]+)*\\.[a-zA-Z]{2,4}");
        }
        public static bool isInt(string intString){
            return System.Text.RegularExpressions.Regex.IsMatch(intString ,"^(\\d{5}-\\d{4})|(\\d{5})$");
        }
        public static bool isUSZip(string zipString){
            return System.Text.RegularExpressions.Regex.IsMatch(zipString ,"^-[0-9]+$|^[0-9]+$");
        }
    }
     


    二、Web.config

    在你的Web.config文件中,在<appSettings>下面增加一个标签:如下

     <appSettings>
        <add key="safeParameters" value="OrderID-int32,CustomerEmail-email,ShippingZipcode-USzip" />
    </appSettings> 

    其中key是<saveParameters>后面的值为"OrderId-int32"等,其中"-"前面表示参数的名称比如:OrderId,后面的int32表示数据类型。

    三、Global.asax

    在Global.asax中增加下面一段:

     
    protected void Application_BeginRequest(Object sender, EventArgs e){
        String[] safeParameters = System.Configuration.ConfigurationSettings.AppSettings["safeParameters"].ToString().Split(',');
        for(int i= 0 ;i < safeParameters.Length; i++){
            String parameterName = safeParameters[i].Split('-')[0];
            String parameterType = safeParameters[i].Split('-')[1];
            isValidParameter(parameterName, parameterType);
        }
    }

    public void isValidParameter(string parameterName, string parameterType){
        string parameterValue = Request.QueryString[parameterName];
        if(parameterValue == null) return;

        if(parameterType.Equals("int32")){
            if(!parameterCheck.isInt(parameterValue)) Response.Redirect("parameterError.htmlx");
        }
        else if (parameterType.Equals("double")){
            if(!parameterCheck.isDouble(parameterValue)) Response.Redirect("parameterError.htmlx");
        }
        else if (parameterType.Equals("USzip")){
            if(!parameterCheck.isUSZip(parameterValue)) Response.Redirect("parameterError.htmlx");
        }
        else if (parameterType.Equals("email")){
            if(!parameterCheck.isEmail(parameterValue)) Response.Redirect("parameterError.htmlx");
        }
    }
     

    以后需要修改的时候我们只需要修改以上三个文件,对整个系统的维护将会大大提高效率,当然你可以根据自己的需要增加其它的变量参数和数据类型。

  • 相关阅读:
    1.python的Helloword
    java实现多个属性排序---按照实体的多种属性值进行排序(ComparableComparator/ComparatorChain)
    Spring Boot 2.X(一):入门篇
    Nginx开启Gzip压缩提升页面加载速度
    QQ第三方授权登录OAuth2.0实现(Java)
    Windows下IIS搭建Ftp服务器
    【Java】支付宝获取人脸采集认证的图片base64格式
    【SpingBoot】spring静态工具类注入问题
    【linux】Tomcat 安装
    【linux】jdk安装及环境变量配置
  • 原文地址:https://www.cnblogs.com/MaxIE/p/335862.html
Copyright © 2020-2023  润新知