I'm trying to authenticate users based on an existing cookie. I've done something similar in the past by using a custom IIdentity and an IPrincipal
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
}
}
I've set up a simple HttpModule to handle the authentication.
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() { }
}
When I disallow anonymous users via the web.config the framework is infinitely redirecting to login.aspx even though HttpContext.Current.Request.IsAuthenticated is true after the authentication event.
This happens for authentication modes Windows, Forms and None.
How do I convince the framework that the request is properly authenticated?
Update:
It turns out there was additional code elsewhere which was calling System.Web.Security.FormsAuthentication.SignOut() so my authentication module was setting the principal, sign out was called and the module would fire again. Thus endless redirect loop.
HttpContext.Current.Request.IsAuthenticated