zoukankan      html  css  js  c++  java
  • SQL 注入 处理

    SqlHelper.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    public static class SqlHelper
    {
        /// <summary>
        /// 替换SQL特殊字符
        /// </summary>
        /// <param name="obj">需序转化的对象</param>
        /// <returns></returns>
        public static string ToMSSQL(this String obj)
        {
            return obj.Replace("'", "''").Replace("[", "[[]").Replace("%", "[%]").Replace("_", "[_]");
        }
    }
    View Code

    在asp.net程序设计中,为了网页的安全我们经常要做防止SQL注入,下面的这个方法只是

            在一定程度上解决了这个问题:

            添加引用:

            using System.Text.RegularExpressions;

            方法引用

            string str=GetSafeSQL(string value);

             方法如下:

             /// <summary>
             /// 过滤SQL非法字符串
             /// </summary>
             /// <param name="value"></param>
             /// <returns></returns>
            public static string GetSafeSQL(string value)
            {
                if (string.IsNullOrEmpty(value))
                    return string.Empty;
                value = Regex.Replace(value, @";", string.Empty);
                value = Regex.Replace(value, @"'", string.Empty);
                value = Regex.Replace(value, @"&", string.Empty);
                value = Regex.Replace(value, @"%20", string.Empty);
                value = Regex.Replace(value, @"--", string.Empty);
                value = Regex.Replace(value, @"==", string.Empty);
                value = Regex.Replace(value, @"<", string.Empty);
                value = Regex.Replace(value, @">", string.Empty);
                value = Regex.Replace(value, @"%", string.Empty);
                return value;
            }

    测试

    create table #test1
    (name varchar(200))
    insert into #test1 values('sdfdsfdsf%')
    insert into #test1 values('sdfdsfdsf')
    
    select * from #test1 where name like '%[%]%'
    drop table #test1
  • 相关阅读:
    转载 | python inferface使用
    gasshopper之python电池输出dict结构
    Tensorflow 2.0 搭建神经网络(局部)
    grasshopper之python电池执行逻辑
    springboot中给属性文件内容加密(application.properties,application.yml)
    使用docker安装rabbitmq
    关于宝塔Linux面板
    winsw来设置window自启服务
    docker的使用,Nginx,Tomcat,Portainer可视化
    内网穿透(windows)
  • 原文地址:https://www.cnblogs.com/xiaoruilin/p/13604046.html
Copyright © 2011-2022 走看看