【问题标题】:How to programmatically set SlidingExpiration to true in mvc Application如何在 mvc 应用程序中以编程方式将 SlidingExpiration 设置为 true
【发布时间】:2012-07-29 12:49:02
【问题描述】:

我有一个带有身份验证表单的 MVC 3 应用程序。当用户选中“记住我”复选框时,auth cookie 必须设置为 2 周,如果没有,则应该设置为 30 分钟。在这两种情况下,都应该打开滑动到期。根据this post,我创建了以下方法,创建自定义身份验证cookie:

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
            1,  //version
            username,
            DateTime.Now, //issue date
            rememberMe ? DateTime.Now.AddDays(14) : DateTime.Now.AddMinutes(30), // exprie date
            rememberMe, // is persistent
            null
            );
        var ecnryptedTicket = FormsAuthentication.Encrypt(ticket);
        HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, ecnryptedTicket);

        cookie.Expires = ticket.Expiration; // to expire cookie together with ticket

        response.Cookies.Add(cookie);

在那之后我如何以编程方式设置滑动到期?

【问题讨论】:

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


    【解决方案1】:

    自从有人问这个问题以来已经有一段时间了,但是我在查看其他内容时遇到了这个问题。如果你想像这样运行混合模式,你可能想用这样的方式自己动手:

    http://cbertolasio.wordpress.com/2010/11/28/dealing-with-idle-clients-and-expiring-sessions-in-asp-net-mvc/

    当从插件发送心跳时,如果需要,请扩展票证。我觉得滑动到期是一个全有或全无的选择。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-14
      • 1970-01-01
      • 2015-09-21
      • 2022-11-05
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      相关资源
      最近更新 更多