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
  • 相关阅读:
    Spring Cloud 五 Feign的文件上传实现
    Spring Cloud 四:服务消费(Feign)【Dalston版】
    Spring Cloud 三:服务消费(Ribbon)【Dalston版】
    Spring Cloud二:服务消费(基础)【Dalston版】
    Spring Cloud 一:服务注册与发现(Eureka)【Dalston版】
    mybatis 单个参数
    医院设置(图论Floyd)
    Mysql学习——安装登录
    字母游戏(搜索)
    子数列连续和
  • 原文地址:https://www.cnblogs.com/xiaoruilin/p/13604046.html
Copyright © 2011-2022 走看看