我試圖基於現有的cookie來驗證用戶。我已經通過使用自定義IIdentity
進行過類似的事情和IPrincipal
ASP.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()
所以我的認證模塊被設置本金,登出叫,模塊將再次觸發。因此無盡的重定向循環。
你確定你的模塊正在踢,並按正確的順序加載? –
是的,我正在模塊中檢查斷點並檢查'HttpContext.Current.Request.IsAuthenticated' –
你在表單上做錯了什麼。運行時並沒有自己重定向到login.aspx。 –