I have an ASP.NET AJAX application that uses forms authentication. There are no postbacks in this application; all server functionality is on Page_Load or in web service calls made directly from the Javascript (via the AJAX toolscripts). On each page load or web service call, the forms authentication ticket is updated by calling a simple utility method:
public static FormsAuthenticationTicket RenewFormsAuthTicket()
{
FormsAuthenticationTicket oldTicket = ((System.Web.Security.FormsIdentity)HttpContext.Current.User.Identity).Ticket;
FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(oldTicket.Version + 1, oldTicket.Name, DateTime.Now, DateTime.Now.AddMinutes(((System.Web.Configuration.AuthenticationSection)System.Configuration.ConfigurationManager.GetSection("system.web/authentication")).Forms.Timeout.Minutes), oldTicket.IsPersistent, oldTicket.UserData);
HttpCookie newCookie = new HttpCookie(System.Web.Security.FormsAuthentication.FormsCookieName, System.Web.Security.FormsAuthentication.Encrypt(newTicket));
HttpContext.Current.Response.Cookies.Add(newCookie);
return newTicket;
}
This "RenewFormsAuthTicket()" method is called from the web service base class's constructor so each individual web service doesn't have to worry about handling the session details. It works great ... except when the web service throws an exception that is intended to go out to the AJAX error handler routine (in the Javascript).
What I found is that when the web service throws an exception, the new auth cookie that was added to the Response.Cookies collection in the above code does not make it into the response. That makes sense to me, actually -- but I still need to keep the forms auth ticket timeout updated because the application is, in fact, being actively used. In cases where the page has a list of items that are being submitted to a web service one at a time, the session sometimes does expire in the middle of making the web service calls, because the exceptions being returned from the web service calls are preventing updates to the authentication cookie.
I thought a solution might be as simple as doing a Response.Flush right after updating the cookie with the above code. This got the cookie updated just fine, but that resulted in the "On Success" Javascript callback function being invoked, and the actual web service exception was not handled.
Does anyone have ideas on getting the forms auth cookie added to the response even when the web service throws an exception?