2013-11-14 107 views
1

我試圖基於現有的cookie來驗證用戶。我已經通過使用自定義IIdentity進行過類似的事情和IPrincipalASP.NET無需登錄頁面的自定義身份驗證

public class CustomIdentity : IIdentity { 

    public string Name { get; set; } 
    public bool IsAuthenticated { get { return true; } } 
    public string AuthenticationType { get { return String.Empty; } } 

} 

public class CustomPrincipal : IPrincipal { 

    private CustomIdentity _identity; 
    private string[] _roles; 

    public IIdentity Identity { 
     get { return _identity; } 
    } 

    public CustomPrincipal(CustomIdentity identity, string[] roles) { 
     _identity = identity; 
     _roles = roles; 
    } 

    public bool IsInRole(string role) { 
     return true; 
     //TODO 
    } 
} 

我已經建立了一個簡單的HTTP模塊來處理身份驗證。

public class Authentication : IHttpModule { 

    public void Init(HttpApplication application) { 
     application.AuthenticateRequest += new EventHandler(Application_AuthenticateRequest); 
    } 

    private void Application_AuthenticateRequest(object sender, EventArgs e) { 
     //TODO: authentication logic here 
     CustomIdentity identity = new CustomIdentity(); 
     CustomPrincipal principal = new CustomPrincipal(identity, new string[] { }); 
     HttpContext.Current.User = principal; 
    } 

    public void Dispose() { } 
} 

當我通過web.config中的框架是無限重定向到login.aspx的,即使HttpContext.Current.Request.IsAuthenticated是認證事件後真正禁止匿名用戶。

這發生在身份驗證模式Windows,Forms和None中。

我該如何說服框架,請求被正確認證?

更新:

原來有額外的代碼的其他地方這是呼喚System.Web.Security.FormsAuthentication.SignOut()所以我的認證模塊被設置本金,登出叫,模塊將再次觸發。因此無盡的重定向循環。

+0

你確定你的模塊正在踢,並按正確的順序加載? –

+0

是的,我正在模塊中檢查斷點並檢查'HttpContext.Current.Request.IsAuthenticated' –

+0

你在表單上做錯了什麼。運行時並沒有自己重定向到login.aspx。 –

回答

0

你是否刪除了asp.net autorization模塊?

<authentication mode="None"> 
.... 
<modules runAllManagedModulesForAllRequests="false"> 
    <remove name="WindowsAuthentication" /> 
    <remove name="PassportAuthentication" /> 
    <remove name="AnonymousIdentification" /> 
    <remove name="FormsAuthentication" /> 
    ... 
    <add name="myauthmodule" type="myauthmodule" /> 

您使用的是UrlAuthorization嗎?

<authorization> 
    <allow roles="Admin" /> 
    <deny users="?" /> 
    <deny users="*" /> 
</authorization> 

我認爲這個問題是由於你混合了自定義自動化和asp.net授權這兩個世界。

如果你想要走這條路,你必須閱讀你的cookie數據,然後做asp.net想:當您在Web.config中設置的認證方式爲Forms一個formauthentication餅乾

//create ticket 
var ticket = new FormsAuthenticationTicket(
     1, // ticket version 
     userName, 
     DateTime.Now, 
     DateTime.Now.Add(timeout), // timeout 
     true, // cookie persistent 
     roles, 
     FormsAuthentication.FormsCookiePath); 

// cookie crypt 
string hash = FormsAuthentication.Encrypt(ticket); 

var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash); 
cookie.Domain = FormsAuthentication.CookieDomain; 

// cookie timeout as ticket timeout 
if (ticket.IsPersistent) 
{ 
    cookie.Expires = ticket.Expiration; 
} 
CurrentContext.Response.Cookies.Add(cookie); 
    .... 
HttpContext.Current.User = new GenericPrincipal(new FormsIdentity(ticket), roles); 
+0

他不必如果模塊正在設置主體。 –

+0

@WiktorZychla它,如果你在web.config中設置爲使用formauthentication – giammin

+0

@giammin我說,行爲表現爲各種形式的認證,而不僅僅是表格 –

0

文件,FormsAuthentication類將根據ASP.NET創建的cookie來負責驗證請求。

由於ASP.NET有自己的公式來創建身份驗證票據,因此任何其他cookie都不會通過身份驗證。

既然你有你的驗證邏輯和公式創建和讀​​取cookies,我認爲你有兩個選擇:

  1. 要麼不使用的web.config,並custimizing你的ASP.NET(的禁用默認認證身份驗證機制高達100%,或者實際上是從頭開始編寫自定義身份驗證)
  2. 使用ASP.NET公式創建身份驗證Cookie。要做到這一點,你需要使用FormsAuthentication.SetAuthenticationCookie方法。
+0

這就是爲什麼我創建一個*自定義* http模塊來處理身份驗證,我沒有試圖使用默認的'FormsAuthentication'。 –

+1

@ Rob2211,你沒有得到它。您已使用自定義Cookie,但基於Web.config的配置,ASP.NET的默認身份驗證正在運行。您需要使用'FormsAuthentication'方法來創建,加密和解密身份驗證Cookie,或者您必須忘記Web.config的身份驗證配置,並自己創建一個。 –

+0

@SaeedNeamati不,他不想理解! :D我放棄了 – giammin

1

In web。配置,設置的成員資格元素上的默認提供給您的自定義供應商的名稱:

... 
<membership Default="YourDefaultProvider"> 
<providers> 
<clear/> 
<add Name="YourDefaultProvider" etc.. /> 
</providers> 
</membership> 
0

這只是一種假設,我不能在此刻驗證,但我相信你無法設置標識與空的名字。您的CustomIdentity構造函數不設置其中一個。

CustomIdentity identity = new CustomIdentity(); 
    identity.Name = "foo"; 

試試這個檢查理論,如果它工作,繼續進一步實現您的自定義驗證邏輯。

+0

謝謝Wiktor,你似乎是唯一一個完全理解這個問題的人。不幸的是,設置名稱沒有任何作用。 –

+0

我已經在VS2012/IIS中驗證了你的方法,它工作正常。用戶被認證爲'foo',沒有重定向到任何頁面,沒有其他奇怪的行爲。這種方法很好,與你的代碼無關,相反,由於某種原因,IIS仍然在後臺使用表單。 –

+0

在另一臺全新的服務器上嘗試此操作,並且僅使用您的模塊和簡單的網頁創建存根解決方案。如果它適合我​​,它必須爲你工作。然後,一步一步找到罪魁禍首。 –

相關問題