日期:2014-05-17  浏览次数:20385 次

如何获取authentication中的forms里的timeout值?
在web.config中:
C# code

    <authentication mode="Forms">
      <forms loginUrl="~/Admin/Login.aspx" defaultUrl="~/Admin/Default.aspx" timeout="30"/>
    </authentication>


生成票据:
C# code

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                                                                                2, // 票据版本号
                                                                                username, // 票据持有者
                                                                                DateTime.Now, //分配票据的时间
                                                                                dt, // 失效时间
                                                                                false, // 是否持久
                                                                                roles, //角色
                                                                                FormsAuthentication.FormsCookiePath);//cookie有效路径



现在请问生成票据里的dt我想在web.config的authentication获取timeout的的值后AddMinutes(value).

比如现在timeout="30",那么dt=DateTime.Now.AddMinutes(30)就会得到30分钟后失效.

现在想问如何获取authentication中的timeout的值?谢谢!

------解决方案--------------------
Configuration conn = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("");
System.Web.Configuration.AuthenticationSection section = (System.Web.Configuration.AuthenticationSection)conn.SectionGroups.Get("system.web").Sections.Get("authentication");
long cookieExpires = System.Convert.ToInt64(section.Forms.Timeout.TotalMinutes);
Response.Write(cookieExpires);