views:

226

answers:

2

Stackoverflow uses OpenId as many other websites. However, I rarely need to provide my OpenId to Stackoverflow while with other OpenId enabled websites, I have to do it once a day or week.

This suggests to me that the expiry of the session is with the website and not the OpenId provider.

Looking at the DotNetOpenId code in ASP MVC, I can see that after a successful authentication by the OpenId provider FormsAuthentication.SetAuthCookie is called with the identifier and a boolean parameter to determine if the cookie should be persisted.

How can I force this cookie to expire, say in 2020 instead of whatever the default value is.

+1  A: 

You can use FormsAuthentication.RenewTicketIfOld() to update the ticket. This method would check if the current ticket has expired and if not, to extend its expiration. You can call this method on every request to your site, which will "slide" th expiration further in future as the user keeps using your site.

Franci Penov
+3  A: 

From MSDN - Explained: Forms Authentication in ASP.NET 2.0:

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
    "userName",
    DateTime.Now,
    new DateTime(2020, 01, 01), // value of time out property
    false, // Value of IsPersistent property
    String.Empty,
    FormsAuthentication.FormsCookiePath);

string encryptedTicket = FormsAuthentication.Encrypt(ticket);

HttpCookie authCookie = new HttpCookie(
                        FormsAuthentication.FormsCookieName, 
                        encryptedTicket);

authCookie.Secure = true;

Response.Cookies.Add(authCookie);
eu-ge-ne
+1 Source code is always useful
J.Hendrix
You don't have to do all that work though. Just change the timeout value in your web.config file: http://msdn.microsoft.com/en-us/library/1d3t3c61.aspx
Andrew Arnott
Indeed, You are right. But Khash may want to set the cookie expiration time programmatically?
eu-ge-ne
This method doesn't work for ASP MVC with DotNetOpenId. However as Andrew said, using the settings in web.config worked! thanks. How can I mark that as the correct answer?
Khash
You can answer the question yourself and mark that as the answer.
Niklas