Problem setting Principal in context inside of HttpModule

G

Greg Hurlman

I have an HttpModule that captures the
FormsAuthenticationModule.Authenticate event, and replaces the
HttpContext principal with a custom principal. The code is
straightforward enough, yet when an ASPX page tries to get this custom
principal it fails - the object I inserted is not there. I've tried
having this module declaration at both the top & bottom of the list in
my web.config... What am I missing?

The code:

public class GCTPrincipalInContext : IHttpModule
{
#region IHttpModule Members

public void Init(HttpApplication context)
{
FormsAuthenticationModule faModule =
(FormsAuthenticationModule)context.Modules["FormsAuthentication"];
faModule.Authenticate += new
FormsAuthenticationEventHandler(faModule_Authenticate);
}

void faModule_Authenticate(object sender, FormsAuthenticationEventArgs
e)
{
if
(HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName]
!= null)
{
FormsAuthenticationTicket ticket =
FormsAuthentication.Decrypt(HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName].Value);

User gctUser =
DataRepository.UserProvider.GetByASPUserId(
(Guid)Membership.GetUser(ticket.Name).ProviderUserKey
)[0];

// I've tried setting the HttpContext.Current.User directly, and I
get
// the same result - not my identity in context.
e.User = new GCTPrincipal(new FormsIdentity(ticket), gctUser);
}
}

public void Dispose()
{
return;
}

#endregion
}
 
S

Shawn Wildermuth

Hello Greg,

I think you're capturing hte wrong event to do your work in. Instead of
capturing your module's Authenticate requestion, capture the HttpApplicaiton's
AuthenticateRequest:

public class GCTPrincipalInContext : IHttpModule
{
#region IHttpModule Members
public void Init(HttpApplication context)
{
context.AuthenticateRequest +=
new EventHandler(faModule_OnAuthenticateRequest);
}
// ...
}

HTH

Thanks,
Shawn Wildermuth
Speaker, Author and C# MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top