PDA

View Full Version : FormsAuth PersistentCookie NOT working!


KenA
06-25-2004, 06:44 AM
Hi, I´m using a FormAuth scheme that is working ok, but the even when the persistent user cookie is set to True, after I close all browser pages and return again, I have a null User.Identity.Name

Parts of my code is below:

After properly checked the login
================================================== ======
FormsAuthentication.Initialize();

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, // Ticket version
this.tbxUsr.Text, // Username to be associated with this ticket
DateTime.Now, // DateTime Issued
DateTime.Now.AddMinutes(120), // DateTime to expire
true, // "true" for a persistent user cookie
userData, // String in a comma delimited form,eg: "Admin,Turista,Parceiro"
FormsAuthentication.FormsCookiePath); // Path cookie is valid for

string hash = FormsAuthentication.Encrypt( ticket );
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,has h );
Response.Cookies.Add( cookie );

string returnUrl = Request.QueryString["ReturnUrl"];
if( returnUrl==null )
{
returnUrl = "Default.aspx";
}
Response.Redirect( returnUrl );
================================================== ======


In Global.asax Application_AuthenticateRequest event I issue the Roles for the GenericPrincipal
================================================== ======
if( HttpContext.Current.User!=null )
{
if( HttpContext.Current.User.Identity.IsAuthenticated )
{
if( HttpContext.Current.User.Identity is FormsIdentity )
{
// Get Forms Identity From Current User
FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;

// The FormsAuthenticationTicket issued in login
// step holds the userData in a string format
FormsAuthenticationTicket ticket = id.Ticket;
string userData = ticket.UserData;

// Issue the Roles for the GenericPrincipal
// that uses String Array instead of string
string[] userRoles = userData.Split(',');
HttpContext.Current.User = new GenericPrincipal(id, userRoles);
}
}
}
================================================== ======


This all works fine, but I lost the User.Identity.Name after closing all browser pages.

Any tips would be very appreciated :)

»»» Ken.Awamura