asp.net - 表单例份验证 - 滑动过期

标签 asp.net asp.net-mvc forms-authentication

我认为我的滑动过期没有发生,人们在几分钟后就不断退出。这是我的设置,为了测试目的,slidingExpiration 设置为“true”,超时我更新为“60”而不是 20。

<authentication mode="Forms">
      <forms name="Lab.ASPXFORMSAUTH" loginUrl="~/Login" enableCrossAppRedirects="true" cookieless="AutoDetect" domain="lab.org" slidingExpiration="true" protection="All" path="/" timeout="60" />
    </authentication>

这是登录代码。如果选择“记住我”,则票证到期时间将为西北一年后,否则将为从现在起 20 分钟。

private static void LoginUser(User user, bool isRememberMe)
        {
            //Forms Authentication
            var expiryDateTime = isRememberMe ? DateTime.Now.AddYears(1) : DateTime.Now.AddMinutes(20);

            var ticket = new FormsAuthenticationTicket(
                    1, // Ticket version
                    user.UserId, // Username associated with ticket
                    DateTime.Now, // Date/time issued
                    expiryDateTime, // Date/time to expire  DateTime.Now.AddYears(1)
                    isRememberMe, // "true" for a persistent user cookie
                    JsonConvert.SerializeObject(user.Roles), // User-data, in this case the roles
                    FormsAuthentication.FormsCookiePath); // Path cookie valid for

            // Encrypt the cookie using the machine key for secure transport
            var hash = FormsAuthentication.Encrypt(ticket);
            var cookie = new HttpCookie(
                FormsAuthentication.FormsCookieName, // Name of auth cookie
                hash); // Hashed ticket

            // Set the cookie's expiration time to the tickets expiration time
            if (ticket.IsPersistent)
            {
                cookie.Expires = ticket.Expiration;
            }

            // Add the cookie to the list for outgoing response
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

看起来我的 web.config 和票证到期时间之间存在一些脱节。你看到我在这里做错了什么吗?谢谢

更新#1:

测试了开发站点,登录(FF 和 chrome),然后 5 分钟后刷新页面,它让我保持登录状态。然后 14 分钟后刷新页面,它将我重定向到登录页面。

测试了产品站点(2 个服务器 - 负载平衡),遵循开发站点刷新间隔,让我保持登录状态。

最佳答案

Scott Hanselman 在这里详细介绍了它。

http://www.hanselman.com/blog/WeirdTimeoutsWithCustomASPNETFormsAuthentication.aspx

您可能需要查看 iisidele 超时

https://technet.microsoft.com/en-us/library/cc771956%28v=ws.10%29.aspx

在 ASP.NET 论坛上寻求帮助来解决该问题。

关于asp.net - 表单例份验证 - 滑动过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29035481/

相关文章:

asp.net-mvc - MVC 中的 Kendo UI 网格,带有条件自定义命令按钮

c# - MVC 模型绑定(bind)到列表 - 仅适用于列表中的第一项

c# - 通过添加 Cookie 验证 Web 请求会抛出错误

c# - 与数据绑定(bind)转发器控件一起使用时如何清理查询字符串?

c# - 如何将 Json 数组反序列化为不同的对象?

asp.net - 在 VS Code 中指定本地主机端口的位置

asp.net - MVC4 托管来自其他 MVC 项目的 View

c# - MVC 表单模型为复杂对象集合返回 null

c# - 当用户路由到关键区域时,ASP.NET MVC 身份验证重定向到登录页面

c# - 更新 FormsAuthenticationTicket 用户数据