I have several apps on a single domain that share the formsauthentication ticket for single sing-on. We also have javascript on each page that will warn the user 2 minutes prior to his session expiration and allow him to logout or extend his session. When the countdown reaches 0, they are automatically logged off. This all works great. However, it fails miserably when a user opens multiple browser windows or tabs to work on the various apps. If window A times out while the user is working in window B, they are signed out of B on the next request.
I have a solution, but I can't seem to implement it. Basically, when the page is rendered, I write out the ticks of the ticket issue date. Then, when auto-logging out, I want to ajax call a handler to see if those ticks match the current formsauthentication ticket ticks. If they don't match, then I know another app has been refreshing the ticket and I won't log them out. The problem is that I can't create a generic handler that doesn't update the ticket because we use slidingexpiration = true.
function CompareTicket(ticks) {
$.getJSON('http://localhost/MyApp/CompareTicket.ashx?t=' + ticks, function (data) {
if (data == 0)
SessionEnd();
else
SessionExtend();
});
}
In the below handler code, data is returned as 0 if the value I'm sending in the querystring matches the ticks of the current ticket. Unfortunately, the mere act of requesting the handler will update the ticket and always send back the new tick count.
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
context.Response.ContentEncoding = Encoding.UTF8;
long ticketTicks = ((FormsIdentity)context.User.Identity).Ticket.IssueDate.Ticks;
if (ticketTicks == 0)
context.Response.Write("0");
else
{
long ticks = 0;
if (long.TryParse(context.Request.QueryString["t"], out ticks))
{
if (ticketTicks == ticks)
context.Response.Write("0");
else
context.Response.Write(ticketTicks.ToString());
}
else
context.Response.Write("0");
}
}
Any ideas on how to get a JSON request from the server without extending the ticket?