In Asp.net, mvc implements the redirection function after timeout pop-up window, and mvc pop-up window

Source: Internet
Author: User

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!

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.