Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器 软件资源

软件开发小程序制作系统集成与运维空间租用硬件开发视频监控技术咨询与支持——联系电话:0311-88999002/88999003

首页 / 网页编程 / ASP.NET / ASP.Net 实现防SQL注入的简单实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace YLUI.Admin.Utility
{
    public class SqlFilter
    {
        public static string Clear(string s)
        {
            if (string.IsNullOrEmpty(s)) return string.Empty;
            s = s.Trim().ToLower();
            s = ClearScript(s);
            s = s.Replace("=", "=");
            s = s.Replace("'", "'");
            s = s.Replace(";", ";");
            s = s.Replace(" or ", " Or ");
            s = s.Replace("select", "Select");
            s = s.Replace("update", "update");
            s = s.Replace("insert", "insert");
            s = s.Replace("delete", "delete");
            s = s.Replace("declare", "declare");
            s = s.Replace("exec", "exec");
            s = s.Replace("drop", "drop");
            s = s.Replace("create", "create");
            s = s.Replace("%", "%");
            s = s.Replace("--", "--");
            return s;
        }
        public static string ClearScript(string s)
        {
            s = s.Replace("<", "&lt;");
            s = s.Replace(">", "&gt;");
            return s;
        }
    }
}

使用方法:

在请求到参数时加入该方法进行过滤,如下即可

string nickname = SqlFilter.Clear(ctt.Request["nickname"]);

string password = SqlFilter.Clear(ctt.Request["password"]);