In Asp.net, mvc implements the redirection function after timeout pop-up window, and mvc pop-up window
To maintain the logon status, you can use cookies to solve this problem.
Assume that the expiration time is 30 minutes, and the verification 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(30); HttpContext.Current.Response.Cookies.Remove("loginInfo"); HttpContext.Current.Response.Cookies.Add(cookie); } } }
However, the page jumps directly without a prompt, which is not very friendly.
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 ('logon time-out, please log on again '); location. href = '{0}' </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 ('logon time-out, please log on again '); location. href = '{0}' </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 a small part of the introduction of the Asp.net mvc implementation timeout pop-up after the jump function, I hope to help you, if you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!