HttpContext.Current.User is always null in Global_AuthenticateRequ

N

Nieko Punt

I implemented forms based authentication in my web app and works great. Now i
want to add role-based authorization. From info on the web I know I have to
handle the AuthenticateRequest. I have code like this:

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
if (!(HttpContext.Current.User == null))
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
if (HttpContext.Current.User.Identity.GetType() ==
typeof(FormsIdentity))
{
// code to add roles here
}
}
}
}


I put a breakpoint at the first if statement in order to see when the event
is fired and what the values are (during login). The event is fired several
times during a single login but HttpContext.Current.User is always null, even
if I succesfully logged in. In contrast, on the web page expressions like
User.Identity.IsAuthenticated work just fine.

What am I missing here?
 
N

Nieko Punt

Correction. The Event signature looks like this:

protected void Global_AuthenticateRequest(Object sender, EventArgs e)
 
J

Joe_Langley

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
if (HttpContext.Current.User != null)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
if (HttpContext.Current.User.Identity is FormsIdentity)
{
FormsIdentity id =
(FormsIdentity)HttpContext.Current.User.Identity;
// Forms Ticket
FormsAuthenticationTicket ticket = id.Ticket;
// Retrieve roles from db
string userData = ticket.UserData;
string[] roles = userData.Split(',');
// Assign new Generic Principal Instance and assign to
Current User
HttpContext.Current.User = new GenericPrincipal(id, roles);
}
}
}
}
 

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,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top