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); } } }