I have a problem with users being kicked out after the forms authentication ticket is renewed and the old one has expired. The first ticket i get when i signed in is:
Ticket: A094D6F0401A5B6D97688198B09F17B03D209............ Ends: Thu, 28 Mar 2013 08:56:33 GMT
And after some time the ticket is renewed and i get this cookie: (The cookie expire when the ticket is expire, so no problem there)
Ticket: 215373E662852AD0CC540AC27F547787............. Ends: Thu, 28 Mar 2013 08:58:17 GMT
This ticket is renewed by a javascript reloader in the background for the user. Now, if i update the page, i will be kicked out, why? When i renew the ticket i use this:
var Id = (FormsIdentity)HttpContext.Current.User.Identity;
var Ticket = Id.Ticket;
var NewAuthTicket = FormsAuthentication.RenewTicketIfOld(Ticket);
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(new FormsIdentity(NewAuthTicket), new[] {""});
if (NewAuthTicket != null && NewAuthTicket.Expiration > Ticket.Expiration)
{
// Create the (encrypted) cookie.
var ObjCookie = new HttpCookie(FormsAuthentication.FormsCookieName,
FormsAuthentication.Encrypt(NewAuthTicket))
{
HttpOnly = true,
Expires = NewAuthTicket.Expiration,
Secure = FormsAuthentication.RequireSSL
};
// Add the cookie to the list for outbound response.
HttpContext.Current.Response.Cookies.Add(ObjCookie);
Ticket = NewAuthTicket;
}
Is there any solution for this?
UPDATE:
When i set the cookie for the first time i use this:
var ExpiryDate = !rememberMe ? DateTime.Now.AddMinutes(cookieTimeoutHour) : DateTime.Now.AddYears(1);
//create a new forms auth ticket
var Ticket = new FormsAuthenticationTicket(2, ui.UserNr.ToString(CultureInfo.InvariantCulture), DateTime.Now, ExpiryDate, true, String.Empty);
//encrypt the ticket
var EncryptedTicket = FormsAuthentication.Encrypt(Ticket);
//create a new authentication cookie - and set its expiration date
var AuthenticationCookie = new HttpCookie(FormsAuthentication.FormsCookieName, EncryptedTicket)
{
Expires = Ticket.Expiration,
HttpOnly = true,
Secure = FormsAuthentication.RequireSSL
};
Current.Response.Cookies.Add(AuthenticationCookie);