基於HttpModule擴充

來源:互聯網
上載者:User

標籤:httpmodule   httphandler   

650) this.width=650;" title="搜狗20141112105809.png" src="http://s3.51cto.com/wyfs02/M02/50/E2/wKioL1RizPPz4KDjAADUpbZzjnc410.jpg" alt="wKioL1RizPPz4KDjAADUpbZzjnc410.jpg" />

一般處理頁面就是HttpHandler地區

-------------------------------封裝類庫

using System;using System.Collections.Generic;using System.Text;using System.Web;using System.Text.RegularExpressions;/* ************************** * 案例功能: * 1,URL地址欄阻止(參數為aspx則跳轉到錯誤頁面) * 2,Form表達阻止(表單的值為aspx則彈出錯誤) * 3,阻止使用session ************************** */namespace HttpModuleDome{    public class MyHttpModule : IHttpModule    {        #region IHttpModule 成員        public void Dispose()        {        }        public void Init(HttpApplication context)        {            context.BeginRequest += new EventHandler(context_BeginRequest);            context.AcquireRequestState += new EventHandler(context_AcquireRequestState);        }        //開始要求階段        void context_BeginRequest(object sender, EventArgs e)        {            HttpApplication application = sender as HttpApplication;            HttpContext context = application.Context;            //Url地址欄阻止            if (context.Request.QueryString.Count > 0)            {                for (int i = 0; i < context.Request.QueryString.Count; i++)                {                    if (context.Request.QueryString[context.Request.QueryString.Keys[i]] == "aspx")                    {                        context.Response.Redirect("http://www.baidu.com");                        context.Response.End();                    }                }            }            //Form表單阻止            if (context.Request.Form.Count > 0)            {                for (int i = 0; i < context.Request.Form.Count; i++)                {                    if (context.Request.Form[context.Request.Form.Keys[i]] == "aspx")                    {                        context.Response.Write("<script>alert(‘錯誤‘);location.href=‘" + context.Request.RawUrl + "‘</script>");                        context.Response.End();                    }                }             }        }        //進入了HttpHandler地區,已經有了session        void context_AcquireRequestState(object sender, EventArgs e)        {            HttpApplication application = sender as HttpApplication;//Global.asax的基類            HttpContext context = application.Context;//封裝了ASP.NET要處理的單次請求的所有資訊            if (context.Session.Count > 0)            {                //context.Response.End();//直接跳過AcquireRequestState之後的請求,結束請求                            }        }        #endregion    }}

 ------------------------------------web.config裡面引用

 <system.web>   <httpModules>   <add name="MyhttpModule" type="HttpModuleDome.MyHttpModule,HttpModuleDome"/>  </httpModules> </system.web>

 ------------------------------------也可以在Global.asax檔案裡面寫

<%@ Application Language="C#" %><script runat="server">    /*     *格式:以Application_開頭     */    //開始要求階段    void Application_BeginRequest(object sender, EventArgs e)    {        // 在應用程式啟動時啟動並執行代碼    }            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,則不會引發該事件。    }       </script>

650) this.width=650;" style="float:none;" title="搜狗20141112110353.png" src="http://s3.51cto.com/wyfs02/M00/50/E4/wKiom1RizbPClYvwAAKBDTGews8172.jpg" alt="wKiom1RizbPClYvwAAKBDTGews8172.jpg" />

650) this.width=650;" style="float:none;" title="搜狗20141112110418.png" src="http://s3.51cto.com/wyfs02/M01/50/E2/wKioL1Rizh-AfkyEAAHP6wsZmXY669.jpg" alt="wKioL1Rizh-AfkyEAAHP6wsZmXY669.jpg" />

本文出自 “程式猿的家--Hunter” 部落格,請務必保留此出處http://962410314.blog.51cto.com/7563109/1575612

基於HttpModule擴充

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.