The error occurs when I am authenticating users using the code below. The code appears in the global.asax.cs file. The error occurs when it gets to the line:
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(httpcook.Value);
public void WindowsAuthentication_OnAuthenticate(Object sender, WindowsAuthenticationEventArgs e)
{
WindowsIdentity id = e.Identity;
string userIdentity = id.Name;
string[] userRoles;
string roleStr = "";
//Make sure user is an authenticated user (use the User class)
User user = new User();
int userID = user.ValidateUser(Utilities.ParseUserLogin(userIde ntity));
//User is not valid
if (userID == 0)
{
Response.Redirect("http://www.google.com");
}
//If user is valid, get roles
user = new User();
userRoles = user.GetUserRoles(userID);
// Create a string to persist the roles
foreach (string role in userRoles)
{
roleStr += role;
}
FormsAuthenticationTicket authTicket =
new FormsAuthenticationTicket(
1,
userIdentity,
DateTime.Now,
DateTime.Now.AddMinutes(60),
false,
roleStr);
//Produces a string containing an encrypted authentication ticket suitable
//for use in an HTTP cookie, given a FormsAuthenticationTicket.
string encTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie httpcook = new HttpCookie("authCookie", encTicket);
Response.Cookies.Add(httpcook);
}
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
HttpCookie httpcook = Context.Request.Cookies["authCookie"];
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(httpcook.Value);
GenericIdentity genIdentity = new GenericIdentity(authTicket.Name);
string[] roles = authTicket.UserData.Split(new char[] {'|'} ); //if more than one role
GenericPrincipal genPrincipal = new GenericPrincipal(genIdentity, roles);
//The principal object thus created would be accessed in the application
//pages before allowing access to the individual functionality
HttpContext.Current.User = genPrincipal;
}
|