2011-06-20 69 views

回答

1

你需要編寫自己的認證模塊 - 看到這個excellent article解釋如何編寫ASP.NET認證模塊,支持基本&摘要式身份驗證。

如果您只考慮表單身份驗證方案,那麼您確實不需要編寫一個表單驗證方案。內置的Forms Authenticate模塊具有很強的可擴展性(並提供了很大的靈活性)。

0

一些谷歌搜索後,我發現this blogpost。它很好地描述瞭如何去做。基本上你需要創建一個自定義HTTP模塊並處理HttpApplication對象的OnAuthenticateRequest事件。

2

Page.User剛剛返回Page.Context.User,應通過模塊或global.asax將其設置在HttpApplication.AuthenticateRequest的處理程序中。您可以提供IPrincipal的自定義實現,然後可以返回IIdentity的自定義實現。

例如:

public class App : HttpApplication 
{ 
    public App() 
    { 
     AuthenticateRequest += App_AuthenticateRequest; 
    } 

    void App_AuthenticateRequest(object sender, EventArgs e) 
    { 
     var cookie = Request.Cookies[FormsAuthentication.FormsCookieName]; 

     if (cookie == null) return; 

     var userData = GetUserData(cookie.Value); 
     var userIdentity = new MyIdentity(userData); 

     Context.User = new MyPrincipal(userIdentity); 
    } 

    private string GetUserData(string value) 
    { 
     try 
     { 
      var ticket = FormsAuthentication.Decrypt(value); 
      return ticket == null ? null : ticket.UserData; 
     } 
     catch (ArgumentException) 
     { 
      Response.Cookies.Remove(FormsAuthentication.FormsCookieName); 
      return null; 
     } 
    } 
}