Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FormsAuthentication.Timeout.TotalMinutes in .NET 3.5

Tags:

c#

.net

c#-3.0

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?

like image 835
Rocky Singh Avatar asked Dec 28 '25 16:12

Rocky Singh


1 Answers

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;
like image 66
Joe Avatar answered Dec 30 '25 06:12

Joe