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?
6 Answers
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);
1 Comment
ChrisW
This is the best answer, but the Timeout property is only available on the FormsAuthentication static class in .Net 4.0 and higher.
Configuration conn = WebConfigurationManager.OpenWebConfiguration("");
AuthenticationSection section = (AuthenticationSection)conn.SectionGroups.Get("system.web").Sections.Get("authentication");
long cookieExpires = System.Convert.ToInt64(section.Forms.Timeout.TotalMinutes);
1 Comment
Rahul More
To get your current project's Web.Config path, type
Request.ApplicationPath inside OpenWebConfiguration("")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