PostAuthenticateRequest 我們能做什麼

來源:互聯網
上載者:User

來看一下MS的說明

注意:此事件在 .NET Framework 2.0 版中是新增的。

當安全模組已建立使用者標識時發生。

命名空間:System.Web
程式集:System.Web(在 system.web.dll 中) 

在2.0中我們使用成員組及Forms驗證來實現使用者的登入,但是成員組所提供的功能有時有限.我們需要能訪問到自己定義的個人資訊時.這時我們就能利用此事件來處理.

當然Context.User對象是實現IPrincipal, IIdentity這二個介面的.因此只要我們自訂的USER類也能實現此介面,我們就實現了一半了.

public class MyUserObject : IPrincipal, IIdentity{         public string Name;           public string PasswordHash;           public string PasswordSalt;      #region IIdentity Members      public string AuthenticationType     {         get         {              return "Froms";         }     }      public bool IsAuthenticated     {         get         {              return true;         }     }      string IIdentity.Name     {         get         {              return this.Name;         }     }      #endregion      #region IPrincipal Members      public IIdentity Identity     {         get         {              return this;         }     }      public bool IsInRole(string role)     {         return false;     }      #endregion}使用者實體替換應該發生在HttpApplication的PostAuthenticateRequest事件發生時,因為此時ASP.NET已經從用戶端得到了使用者憑證Cookie並進行瞭解密和校正。我們既可以編寫一個HttpModule來處理PostAuthenticateRequest事件,也可以在Global.asax檔案中添加事件處理器。這裡為了簡單,我們選擇在Global.asax中添加如下事件處理器:

void Application_PostAuthenticateRequest(object sender, EventArgs e){     HttpApplication application= (HttpApplication)sender;     if(application.Context.User.Identity.Name != "") // 登入成功後     {         MyUserObject user ="自己取出對象的處理";         application.Context.User = user;             }}下面你就可以在其他頁面訪問到自訂的使用者實體物件了.MyUserObject user = HttpContext.Current.User as MyUserObject

聯繫我們

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