來看一下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