我刚刚使用FormsAuthentication,我想在web配置中使用表单身份验证标记的timeout属性值.在4.0中我们可以通过FormsAuthentication.Timeout.TotalMinutes(参考:FormsAuthenticationTicket.expiration v web.config值超时)获取此信息.您能告诉我如何在.NET 2.0中获得相同的内容吗?
在Microsoft的Connect站点上查看此问题.它被关闭为"Will Not Fix",但看起来它已经在.NET 4中修复了.
在.NET 2.0或3.x中执行此操作的一种方法是发出并检查FormsAuthentication票证:
FormsAuthentication.SetAuthCookie("user", false);
HttpCookie cookie = (HttpCookie)(Request.Cookies[FormsAuthentication.FormsCookieName]);
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
int timeoutInMinutes = (ticket.Expiration - ticket.IssueDate).TotalMinutes;
Run Code Online (Sandbox Code Playgroud)
另一种是使用配置API:
Configuration config = Configuration.OpenWebConfiguration(HttpRuntime.AppDomainAppPath);
AuthenticationSection section =
(AuthenticationSection)config.GetSection("system.web/authentication");
int timeout = section.Forms.Timeout.TotalMinutes;
Run Code Online (Sandbox Code Playgroud)