2011-06-20 61 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; 
     } 
    } 
}