3

I was just working with FormsAuthentication and I wanted the value of timeout property of form authentication tag in web config. In 4.0 we can get this via FormsAuthentication.Timeout.TotalMinutes (ref: FormsAuthenticationTicket.expiration v web.config value timeout) Can you let me know how can I get the same in .NET 2.0?

Community
  • 1
  • 1
Rocky Singh
  • 15,128
  • 29
  • 99
  • 146

1 Answers1

6

Take a look at this issue on Microsoft's Connect site. It was closed as "Won't Fix", but it looks like it's been fixed in .NET 4.

One way of doing it in .NET 2.0 or 3.x is to issue and inspect a FormsAuthentication ticket:

FormsAuthentication.SetAuthCookie("user", false);
HttpCookie cookie = (HttpCookie)(Request.Cookies[FormsAuthentication.FormsCookieName]);
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
int timeoutInMinutes = (ticket.Expiration - ticket.IssueDate).TotalMinutes; 

Another is to use the configuration APIs:

Configuration config = Configuration.OpenWebConfiguration(HttpRuntime.AppDomainAppPath);
AuthenticationSection section = 
    (AuthenticationSection)config.GetSection("system.web/authentication");
int timeout = section.Forms.Timeout.TotalMinutes;
Joe
  • 122,218
  • 32
  • 205
  • 338