在web.config中:    <authentication mode="Forms">
      <forms loginUrl="~/Admin/Login.aspx" defaultUrl="~/Admin/Default.aspx" timeout="30"/>
    </authentication>生成票据: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的值?谢谢!

解决方案 »

  1.   

    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);
      

  2.   

    另外也可以使用XmlDocument读取web.config进行读取。
    但这个值不一定是有效的值
      

  3.   

    为什么我这样写输入的都是30呢(不管我的 timeout="1"还是 timeout="10")?
      

  4.   

    为什么我这样写输的都是30呢(不管我的 timeout="1"还是 timeout="10")?
      

  5.   

    不好意思.忘记给一个值了.penWebConfiguration("~/web.config")