Application_AuthenticateRequest Forms Authentication Roles not being assigned
I am implementing Forms Authentication by creating a forms authentication ticket in my sign in page (storing roles as delimited string in userdata), encrypting it, and saving as a cookie. In the Global.asax Application_AuthenticateRequest() I load the cookie decrypt it, split out the roles from the userdata, create a string array, create a Generic principal and assign the roles array. This is then assigned to the Context.User object. However when I load a page (other than the sign in page) the httpContext.Current.User has identity but the roles array is empty and as such IsInRole("XXX") always returns false. If I copy the code out of Application_AuthenticateRequest() into the page load event it all works fine (m_roles is populated). What do you guys think I am doing wrong? I have included the Application_AuthenticateRequest() code below (with the roles array hard coded in case the split was causing the issue). Any assistance is appreciated.
void Applicaiton_AuthenticateRequest(object sender, EventArgs e)
{
//Get authentication cookie
string sCookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = Context.Request.Cookies[sCookieName];
//If the cookie cannot be found then do not issue the ticket
if (authCookie == null)
{
return;
}
//Get the tictet and rebuild the principal and identity
FormsAuthenticationTicket authTicket =
FormsAuthentication.Decrypt(authCookie.Value);
//Get the Roles
string[] sRoles = { "Group Administrator", "Other" };// authTicket.UserData.Split(new char[] { '|' });
//Build the identity
System.Security.Principal.GenericIdentity userIdentity =
new System.Security.Principal.GenericIdentity(authTicket.Name);
//Build the Principal
System.Security.Principal.GenericPrincipal userPrincipal =
new System.Security.Principal.GenericPrincipal(userIdentity, sRoles);
Context.User = userPrincipal;
}

