7

I'm adding a logout expiration alert to my application and would like to access my web.config forms authentication "timeout" value from my code. Any way i could do this?

aaron
  • 101
  • 1
  • 5

6 Answers6

19

I think you can read it from the FormsAuthentication static class methods, which would be better than doing it by reading the web.config directly as you may be inheriting authentication settings from a higher level web.config.

var authTicket = new FormsAuthenticationTicket(user.EmailAddress, true, (int)FormsAuthentication.Timeout.TotalMinutes);
RasikaSam
  • 5,363
  • 6
  • 28
  • 36
cjk
  • 45,739
  • 9
  • 81
  • 112
  • 3
    This is the best answer, but the Timeout property is only available on the FormsAuthentication static class in .Net 4.0 and higher. – ChrisW May 08 '12 at 18:43
7

You can access the web.config's timeout value in:

FormsAuthentication.Timeout.TotalMinutes

I don't know since when it's available, I'm using .NET 4.5.

Luiz Damim
  • 3,803
  • 2
  • 27
  • 31
4
 Configuration conn = WebConfigurationManager.OpenWebConfiguration("");

            AuthenticationSection section = (AuthenticationSection)conn.SectionGroups.Get("system.web").Sections.Get("authentication");



            long cookieExpires = System.Convert.ToInt64(section.Forms.Timeout.TotalMinutes);
Will
  • 655
  • 6
  • 16
  • To get your current project's Web.Config path, type `Request.ApplicationPath` inside `OpenWebConfiguration("")` – Rahul More Dec 19 '13 at 12:22
1

You can access it from your Javascript using the following:

var expireTime = <%= FormsAuthentication.Timeout.TotalMinutes %>;
Colin
  • 1,758
  • 1
  • 19
  • 24
0

This Code will give you timeout in minutes from AuthenticationSection section present in your current project's Web.Config file,

Configuration conn = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
AuthenticationSection section = (AuthenticationSection)conn.SectionGroups.Get("system.web").Sections.Get("authentication");
FormsAuthenticationConfiguration currentForms = section.Forms;
int timeout = currentForms.Timeout.Minutes;
txtAppSessionTimeout.Text = timeout.ToString();

Please mark it as correct if you found this answer is correct

Rahul More
  • 615
  • 3
  • 13
  • 41
0

You can parse it directly from the web.config file.

great_llama
  • 11,481
  • 4
  • 34
  • 29