ASP code instance to implement jump function after timeout popup

Source: Internet
Author: User
This article mainly introduces the MVC in ASP. NET after the timeout pop-up window jump function, very good, with reference value, the need for friends can refer to the next

In order to keep the login status, you can use cookies to solve this problem

Assuming that the expiration time is 30 minutes, the checksum occurs on the server, with the help of the filter, you can write

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 (+);        HttpContext.Current.Response.Cookies.Remove ("Logininfo");        HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (cookie);}}}  

But the page jumps directly, also does not have a hint, appears not very friendly, can 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 (' login timeout, login again '), location.href= ' {0} ' </script>", "/admin/login/index")        };      }      else      {        cookie. Expires = DateTime.Now.AddMinutes (+);        HttpContext.Current.Response.Cookies.Remove ("Logininfo");        HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (cookie);}}}  

But what if it's an AJAX request?

public class Powerfilter:authorizeattribute {public override void Onauthorization (AuthorizationContext Filtercontex      T) {var cookie = httpcontext.current.request.cookies["Logininfo"]; if (null = = cookie) {if (!filtercontext.httpcontext.request.isajaxrequest ()) {Filtercontext.re Sult = new Contentresult () {Content = string.        Format ("<script>alert (' login timeout, re-login '); location.href= ' {0} ' </script>", "/admin/login/index")}; } else {filtercontext.result = new Jsonresult () {Data = new {logoff = True,l Ogurl = "/admin/login/index"}, ContentType = null, contentencoding = NULL, JSONREQUESTBE        Havior = Jsonrequestbehavior.allowget}; }} else {cookie.        Expires = DateTime.Now.AddMinutes (30);        HttpContext.Current.Response.Cookies.Remove ("Logininfo"); Httpcontext.current.rEsponse.      Cookies.add (cookie); }    }  }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.