HttpModule,對ASP.NET的事件處理進行過濾,幹預)

來源:互聯網
上載者:User

HttpModule通過對HttpApplication對象的一系列事件的處理來對HTTP處理管道施加影響。這些事件要在HttpModule的Init方法中進行註冊,包括:BeginRequest,AuthenticateRequest, AuthorizeRequest, ResolveRequestCache, AcquireRequestState, PreRequestHandlerExecute, PostRequestHandlerExecute, ReleaseRequestState, UpdateRequestCache, EndRequest。

using System;
using System.Web;
using System.Security.Principal;

namespace xumh
{
    /// <summary>
    /// 
    /// 實現HttpModule:
    /// 1、編寫類實現IHttpModule
    /// 2、編譯為類庫:csc /t:library testhttpmodule.cs
    /// 3、要讓你的WEB頁面使用它,必須在web.config中註冊。
    /// <httpModules> 
    /// <add name="xumhHttpModule" type="xumh.testHttpModule,testHttpModule"/>
    /// <add name="隨便起個名字" type="空間.類名,dll檔案不帶副檔名 />
    /// </httpModules>
    /// 4、本HttpModule處理註冊,為方便測試需要禁用匿名訪問,如下:
    ///        <authorization>
    ///             <deny users="?"/>
    ///        </authorization>
    /// </summary>
    public class testHttpModule : IHttpModule
    {
        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void Init(HttpApplication context)
        {//裡面我們可以註冊很多的事件
            context.AuthenticateRequest += new EventHandler(context_AuthenticateRequest);
        }

        //AuthenticateRequest
        void context_AuthenticateRequest(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            HttpContext context = (HttpContext)app.Context;
            if ( app.Request["userid"] == null || app.Request["password"] ==null)
            {
                context.Response.Write("使用者名稱或者密碼為空白,驗證失敗!");
                app.Response.End();
            }
            //
            string userid = app.Request["userid"].ToString();
            string password = app.Request["password"].ToString();
            string[] roles = AuthenticateAndGetRoles(userid, password);//擷取使用者權限表
            if (roles==null || roles.GetLength(0) ==0)
            {
                app.Response.Write("使用者名稱或者密碼錯誤,驗證失敗!");
                app.CompleteRequest();//終止一個http請求
            }
            GenericIdentity identity = new GenericIdentity(userid, "CustomAuthentication");
            context.User = new GenericPrincipal(identity, roles);

        }

        //AuthenticateAndGetRoles
        private string[] AuthenticateAndGetRoles(string userid, string password)
        {
            string[] roles = null;
            if (userid.Equals("xuminghui") && password.Equals("1234"))
            {
                roles = new string[1];
                roles[0] = "Administrator";
            }
            else if (userid.Equals("haohao") && password.Equals("1017"))
            {
                roles = new string[1];
                roles[0] = "User";
            }
            return roles;
        }

    }

}

詳細處理流程見

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.