asp.net mvc 过滤XSS跨站脚本攻击

web.config增加httpModules节点
<httpModules>
      <add name="HttpAccessInterceptModule" type="Org.Core.Commons.HttpAccessInterceptModule, Org.Core.Commons"/>
</httpModules>
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;

namespace Org.Core.Commons
{
   
    /// <summary>
    /// http访问拦截器模块
    /// 1.过滤危险关键词
    /// 2.增加安全Header
    /// </summary>
    public class HttpAccessInterceptModule : IHttpModule
    {
   
        private static List<string> _RegexWords;
        static HttpAccessInterceptModule()
        {
   
            _RegexWords = new List<string>()
            {
   
                @"<[^>]+>'", 
                @"</[^>]+>'",
                @"<[^>]+?style=[\w]+?:expression\(|\b(alert|confirm|prompt|window|location|eval|console|debugger|new|Function|var|let)\b|^\+/v(8|9)|<[^>]*?=[^>]*?&#[^>]*?>|\b(and|or)\b.{1,6}?(=|>|<|\bin\b|\blike\b)|/\*.+?\*/|<\s*script\b|\bEXEC\b|UNION.+?SELECT|UPDATE.+?SET|INSERT\s+INTO.+?VALUES|(SELECT|DELETE).+?FROM|(CREATE|ALTER|DROP|TRUNCATE)\s+(TABLE|DATABASE)"
            };
            string[] keyWords = {
    };
            //{"'", "alert", "script","case","catch","const","continue","debugge","delete","export*","final","finally","for","function","goto","if","implements","import*","return","switch","synchronized","throw","throws","transient","try","break"}
            //new string[] { "select", "insert", "update", "delete", "drop", "truncate" };

            _RegexWords.AddRange(keyWords.Select(o =
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值