.net Access Forms authentication “timeout” value in code

后端 未结 6 1567
小蘑菇
小蘑菇 2021-02-09 18:39

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条回答
  • 2021-02-09 19:20

    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

    0 讨论(0)
  • 2021-02-09 19:21

    You can access it from your Javascript using the following:

    var expireTime = <%= FormsAuthentication.Timeout.TotalMinutes %>;
    
    0 讨论(0)
  • 2021-02-09 19:27
     Configuration conn = WebConfigurationManager.OpenWebConfiguration("");
    
                AuthenticationSection section = (AuthenticationSection)conn.SectionGroups.Get("system.web").Sections.Get("authentication");
    
    
    
                long cookieExpires = System.Convert.ToInt64(section.Forms.Timeout.TotalMinutes);
    
    0 讨论(0)
  • 2021-02-09 19:32

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

    0 讨论(0)
  • 2021-02-09 19:35

    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.

    0 讨论(0)
  • 2021-02-09 19:37

    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);
    
    0 讨论(0)
提交回复
热议问题