【问题标题】:ASP.NET Forms Authentication ExpiryASP.NET 表单身份验证到期
【发布时间】:2012-11-13 22:39:05
【问题描述】:

有人可以向我解释一下 ASP.NET 表单身份验证的工作原理吗,因为我似乎不明白,而且我一直在退出。

就目前而言,我有用户名、密码和“保持登录”复选框。我将根据这些值创建票证和 cookie,如下所示:

    // Create ticket
    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,email,DateTime.UtcNow,DateTime.UtcNow.AddMinutes(30),remember,String.Empty);

    // Encrypt ticket
    string cookie_contents = FormsAuthentication.Encrypt(ticket);

    // Create cookie
    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,cookie_contents);

    if (remember) {
        cookie.Expires = DateTime.UtcNow.AddDays(90);
    }

    cookie.Path = FormsAuthentication.FormsCookiePath;
    cookie.Secure = true;

    // Add cookie to response
    Response.Cookies.Add(cookie); 

我希望使用此代码登录我的网站并假设我选中“保持登录状态”并保持登录状态至少 90 天?

但是我看到的是,我在首次登录后至少 30 分钟被注销(这是为票预留的时间?)。

cookie 到期和票证到期之间有什么区别,以及如何让自己保持签名。我需要为 cookie 和票证都设置 90 天吗?

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    如果可以避免,请不要直接操作 cookie。您可以使用FormsAuthentication.SetAuthCookie(username, persistent) 让用户登录。这里的持久意味着“不使用会话cookie”。

    然后您应该在

    下的 web.config 中指定 cookie 过期时间
     <system.web>
       <authentication mode="Forms">
                 <forms timeout="50000000" slidingExpiration="true"/>
       </authentication>
     </system.web>
    

    其中滑动到期意味着将为每个请求更新 cookie。超时以分钟为单位,所以这个例子相当高:)

    看看这个问题和 Scott Gu 博客的链接:Forms Authentication Cookie Expiration

    【讨论】:

    • cookie.Secure 与 SSL 怎么样?我也将 requireSSL 添加到了表单标签中。滑动过期也会更新活动,但是没有活动怎么办?即我将关闭浏览器,稍后再回来......
    • yes requireSSL="true" 在上面的那个标签中将使它成为一个安全的cookie。另一个好用的标签是 enableCrossAppRedirects="false"
    • 好的,我想我明白了,将超时设置为 30 天之类的疯狂时间,然后滑动到期应该处理其余部分 + 如果他们没有在该时间范围内登录,它就会到期。
    • Scott Gu 提供的博客链接是最好的方法
    • 如果不使用会话cookie,有什么用?
    猜你喜欢
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 2014-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多