Asp.net 手动续订表单身份验证票证:

Asp.net 手动续订表单身份验证票证:,asp.net,forms-authentication,httpcookie,Asp.net,Forms Authentication,Httpcookie,表单身份验证票证过期过快的另一个问题。 我需要将滑动过期设置为true。我读过论坛,了解到了失去精确性的问题,即只有在过期时间过半后才发出请求时,票据才会更新 问题是: 在我的网络配置中,我有以下内容: <authentication mode="Forms"> <forms timeout="20" name="SqlAuthCookie" protection="All" slidingExpiration="true" /> <

表单身份验证票证过期过快的另一个问题。 我需要将滑动过期设置为true。我读过论坛,了解到了失去精确性的问题,即只有在过期时间过半后才发出请求时,票据才会更新

问题是: 在我的网络配置中,我有以下内容:

    <authentication mode="Forms">
        <forms timeout="20" name="SqlAuthCookie" protection="All" slidingExpiration="true" />
    </authentication>
    <sessionState timeout="20" />
    <authorization>
我的问题是:

  • 在每次请求时重置cookie是错误的还是可接受的解决方案
  • 为什么它仍然不起作用?新的票似乎永远不会更新
  • 对于用户的表单身份验证过早过期这一事实,我是否应该调查其他原因 谢谢,,
    在这方面,

    表单身份验证cookie只有在过期时间过半后才会自我更新

    来自Microsoft:

    如果网页在过期时间的一半之前被访问, 票据到期时间将不会重置。例如,如果有任何网站 页面在下午5:04 00:00再次访问,cookies和票据 超时时间将不会重置

    以防止性能受损,并避免多个浏览器 警告对于启用了cookie警告的用户,cookie是 超过指定时间一半时更新

    这可能是你的问题。如果您的客户在9分钟后访问您的站点,并且在10分钟内不再访问该站点,那么他们将超时。即使会话超时设置为20分钟,也会发生这种情况


    没有必要像现在这样手动更新您的票证。您只需要启用滑动过期。如果“特定时间的一半”规则不适用于您,则您必须考虑其他解决方案。

    您使用的是什么版本的framework和IIS?项目的目标framework是4.0。IIS版本是7,但我正在visual studio 2010内置WebServer上进行测试。我认为应该在forms标记中指定domain属性。无需在每个请求上写入续订cookie请详细说明不应在每个请求上续订cookie的原因。我只是查看了关于domain属性的文档,我不认为这有助于解决这个问题。谢谢,我们用了同样的问题。在这篇文章之前我遇到了微软的文章。大部分信息都在这里。
        void context_AcquireRequestState(object sender, EventArgs e)
        {
            HttpContext ctx = HttpContext.Current;
            ResetAuthCookie(ctx);
         }
    
                private void ResetAuthCookie(HttpContext ctx)
        {
            HttpCookie authCookie = ctx.Request.Cookies[FormsAuthentication.FormsCookieName];
            if (authCookie == null)
                return;
    
            FormsAuthenticationTicket ticketOld = FormsAuthentication.Decrypt(authCookie.Value);
            if (ticketOld == null)
                return;
    
            if (ticketOld.Expired)
                return;
    
            FormsAuthenticationTicket ticketNew = null;
            if (FormsAuthentication.SlidingExpiration)
               ticketNew = FormsAuthentication.RenewTicketIfOld(ticketOld);
    
            if (ticketNew != ticketOld)
                StoreNewCookie(ticketNew, authCookie, ctx);
        }
    
        private void StoreNewCookie(FormsAuthenticationTicket ticketNew, HttpCookie authCookie, HttpContext ctx)
        {
            string hash = FormsAuthentication.Encrypt(ticketNew);
            if (ticketNew.IsPersistent)
                authCookie.Expires = ticketNew.Expiration;
    
            authCookie.Value = hash;
            authCookie.HttpOnly = true;
    
            ctx.Response.Cookies.Add(authCookie);
        }