Home  >  Article  >  Backend Development  >  Example of asp code to implement jump function after timeout pop-up window

Example of asp code to implement jump function after timeout pop-up window

Y2J
Y2JOriginal
2017-05-04 11:01:531495browse

This article mainly introduces the function of jumping after timeout pop-up window in MVC in Asp.net. It is very good and has reference value. Friends who need it can refer to it

In order to maintain the login status, you can Use cookies to solve this problem

Assume the expiration time is 30 minutes, and the verification occurs on the server. With the help offilter, you can write like this

 public class PowerFilter : AuthorizeAttribute
  {
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
      var cookie = HttpContext.Current.Request.Cookies["loginInfo"];
      if(null == cookie)
      {
        filterContext.Result = new RedirectResult("/admin/login/index");
      }
      else
      {
        cookie.Expires = DateTime.Now.AddMinutes(30);
        HttpContext.Current.Response.Cookies.Remove("loginInfo");
        HttpContext.Current.Response.Cookies.Add(cookie);
      }
    }
  }

but the page will jump directly After transferring, there is no prompt, which seems not very friendly. It can be like this

public class PowerFilter : AuthorizeAttribute
  {
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
      var cookie = HttpContext.Current.Request.Cookies["loginInfo"];
      if(null == cookie)
      {
        filterContext.Result = new ContentResult()
        {
          Content = string
          .Format("<script>alert(&#39;登录超时,请重新登录&#39;);location.href=&#39;{0}&#39;</script>","/admin/login/index")
        };
      }
      else
      {
        cookie.Expires = DateTime.Now.AddMinutes(30);
        HttpContext.Current.Response.Cookies.Remove("loginInfo");
        HttpContext.Current.Response.Cookies.Add(cookie);
      }
    }
  }
}

But what if it is an ajax request?

public class PowerFilter : AuthorizeAttribute
  {
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
      var cookie = HttpContext.Current.Request.Cookies["loginInfo"];
      if(null == cookie)
      {
        if(!filterContext.HttpContext.Request.IsAjaxRequest())
        {
          filterContext.Result = new ContentResult()
          {
            Content = string
                 .Format("<script>alert(&#39;登录超时,请重新登录&#39;);location.href=&#39;{0}&#39;</script>","/admin/login/index")
          };
        }
        else
        {
          filterContext.Result = new JsonResult()
          {
            Data = new { logoff = true,logurl = "/admin/login/index" },
            ContentType = null,
            ContentEncoding = null,
            JsonRequestBehavior = JsonRequestBehavior.AllowGet
          };
        }
      }
      else
      {
        cookie.Expires = DateTime.Now.AddMinutes(30);
        HttpContext.Current.Response.Cookies.Remove("loginInfo");
        HttpContext.Current.Response.Cookies.Add(cookie);
      }
    }
  }

The above is the detailed content of Example of asp code to implement jump function after timeout pop-up window. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn