【问题标题】:How to extend AuthCookie expiration time when page refresh is refreshed?页面刷新时如何延长AuthCookie过期时间?
【发布时间】:2017-07-28 20:58:04
【问题描述】:

我在这样的登录操作方法中设置了一个 AuthCookie

FormsAuthentication.SetAuthCookie(username,true);

在 Global.asax 文件中我放了刷新代码,(这将在每个动作方法认证后执行)

protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
 string username = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;

 HttpCookie cookie = FormsAuthentication.GetAuthCookie(username, true);
 var ticket = FormsAuthentication.Decrypt(cookie.Value);
 var newticket = new FormsAuthenticationTicket(ticket.Version,                                                                    
 ticket.Name,ticket.IssueDate,                                       
 ticket.Expiration,true,"new user data",ticket.CookiePath);

 cookie.Value = FormsAuthentication.Encrypt(newticket);
 cookie.Expires = newticket.Expiration.AddHours(24);
 HttpContext.Current.Response.Cookies.Set(cookie);
}

但它不起作用,并且在一小段时间后退出,尽管当我通过浏览器设置检查该 cookie 时,过期时间设置正确。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-5


    【解决方案1】:

    可能在 Web.config' 中为记录的用户/会话的超时设置了一个值,如下所示:

    <authentication mode="Forms">
        <forms loginUrl="~/User/Login" timeout="10" />
    </authentication>
    

    您可以从这里获得有关表单会话的一些有用信息: Forms authentication timeout vs sessionState timeout

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-09
      • 2015-06-26
      • 1970-01-01
      • 2021-08-16
      相关资源
      最近更新 更多