如何在MVC 5应用程序中设置滑动到期 [英] How to set sliding expiration in MVC 5 application

查看:86
本文介绍了如何在MVC 5应用程序中设置滑动到期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用MVC 5构建的应用程序,我们有一个场景,即使用户仍然在文本框上打字,当默认会话超时已经过去时,用户被重定向到登录页面。我希望用户只有在页面闲置超过15分钟时才会被重定向到登录页面,但是当用户非常闲置时却不会。在Web表单中,我们使用在配置文件中将滑动到期时间设置为true,但这在MVC中不起作用。我将不胜感激。



我的尝试:



登录行动:



I have an application built using MVC 5, we have a scenario where users are redirected to the login page when the default session timeout has elapsed even while the user was still typing on a text box. I want users to be redirected to login page only when a page has been left idle for more than 15 minutes but not when users are very. In web forms, we use to set sliding expiration to true in the config file, but that doesn't work in MVC. I will appreciate any assistance.

What I have tried:

Login Action:

  [HttpPost]
     [AllowAnonymous]

public ActionResult Login(LoginViewModel model, string returnUrl, string command)
   {
      string decodedUrl = "";
      if (!string.IsNullOrEmpty(returnUrl))
          decodedUrl = Server.UrlDecode(returnUrl);
          FormsAuthentication.SetAuthCookie(model.Email, false);

         var authTicket = new FormsAuthenticationTicket(1, user.Email,
         DateTime.Now, DateTime.Now.AddMinutes(15), false, user.Roles);
         string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
         var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName,
         encryptedTicket);
         HttpContext.Response.Cookies.Add(authCookie);
         authCookie.Expires = authTicket.Expiration;

          if (Url.IsLocalUrl(decodedUrl))
              {
                   return Redirect(decodedUrl);
              }
             else
              {
               return RedirectToAction("analytics", "dashboard");
             }
   }





全球ASAX代码:





Global ASAX Code:

protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
        {
            try
            {
        var authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
                if (authCookie != null)
                {
                    FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
                    if (authTicket != null && !authTicket.Expired)
                    {
                        var roles = authTicket.UserData.Split(',');
                        HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(new FormsIdentity(authTicket), roles);
                    }
                }
            }
            catch(Exception ex)
            {
                
            }
        }

推荐答案

当用户非常是什么时?



如果它已过期,它已过期。



如果超时时间太短,那么平均延伸它。
When users "are very" what?

If it's expired, it's expired.

If the timeout is too short, on average, then extend it.


这篇关于如何在MVC 5应用程序中设置滑动到期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆