C#跨站脚本防注入SXX

//Global.asax里面插入代码
<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e)
    {
        //在应用程序启动时运行的代码

    }

    void Application_End(object sender, EventArgs e)
    {
        //在应用程序关闭时运行的代码

    }

    void Application_Error(object sender, EventArgs e)
    {
        //在出现未处理的错误时运行的代码

    }

    void Session_Start(object sender, EventArgs e)
    {
        //在新会话启动时运行的代码

    }

    void Session_End(object sender, EventArgs e)
    {
        //在会话结束时运行的代码。 
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时,才会引发 Session_End 事件。如果会话模式 
        //设置为 StateServer 或 SQLServer,则不会引发该事件。

        WebContel_damu m_delsession = new WebContel_damu();
        m_delsession.CheckLogin_del(Session["user"].ToString());
    }

    void Application_BeginRequest(object sender, EventArgs e)
    {

        string q = "

您提交的参数不合法,请确认后再次提交!
"
; if (Request.Cookies != null) { if (Nvs_Net_Safe.CookieData()) { Response.Write(q); Response.End(); } } if (Request.UrlReferrer != null) { if (Nvs_Net_Safe.referer()) { Response.Write(q); Response.End(); } } if (Request.RequestType.ToUpper() == "POST") { if (Nvs_Net_Safe.PostData()) { Response.Write(q); Response.End(); } } if (Request.RequestType.ToUpper() == "GET") { if (Nvs_Net_Safe.GetData()) { Response.Write(q); Response.End(); } } } </script>
//Nvs_Net_Safe类里面插入代码
using System;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;
public class Nvs_Net_Safe
{

   // private const string StrRegex = @"\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)";
    private const string StrRegex = @"<[^>]+?style=[\w]+?:expression\(|\b(alert|confirm|prompt)\b|^\+/v(8|9)|<[^>]*?=[^>]*?&#[^>]*?>|\b(and|or)\b.{1,6}?(=|>|<|\bin\b|\blike\b)|/\*.+?\*/|<\s*script\b|<\s*img\b|\bEXEC\b|UNION.+?SELECT|UPDATE.+?SET|INSERT\s+INTO.+?VALUES|(SELECT|DELETE).+?FROM|(CREATE|ALTER|DROP|TRUNCATE)\s+(TABLE|DATABASE)";
    public static bool PostData()
    {
        bool result = false;

        for (int i = 0; i < HttpContext.Current.Request.Form.Count; i++)
        {
            result = CheckData(HttpContext.Current.Request.Form[i].ToString());
            if (result)
            {
                break;
            }
        }
        return result;
    }

 
    public static bool GetData()
    {
        bool result = false;

        for (int i = 0; i < HttpContext.Current.Request.QueryString.Count; i++)
        {
            result = CheckData(HttpContext.Current.Request.QueryString[i].ToString());
            if (result)
            {
                break;
            }
        }
        return result;
    }


    public static bool CookieData()
    {
        bool result = false;
        for (int i = 0; i < HttpContext.Current.Request.Cookies.Count; i++)
        {
            result = CheckData(HttpContext.Current.Request.Cookies[i].Value.ToLower());
            if (result)
            {
                break;
            }
        }
        return result;
    
    }
    public static bool referer()
    {
        bool result = false;
        return result = CheckData(HttpContext.Current.Request.UrlReferrer.ToString());
    }

    public static bool CheckData(string inputData)
    {
        if (Regex.IsMatch(inputData, StrRegex))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

  
}

你可能感兴趣的:(C#跨站脚本防注入SXX)