欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Asp.net 中mvc 實(shí)現(xiàn)超時(shí)彈窗后跳轉(zhuǎn)功能

 更新時(shí)間:2017年02月10日 16:18:47   作者:行走即歌  
這篇文章主要介紹了Asp.net 中mvc 實(shí)現(xiàn)超時(shí)彈窗后跳轉(zhuǎn)功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下

為了實(shí)現(xiàn)保持登錄狀態(tài),可以用cookie來解決這一問題

假設(shè)過期時(shí)間為30分鐘,校驗(yàn)發(fā)生在服務(wù)器,借助過濾器,可以這樣寫

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

但是頁面直接跳轉(zhuǎn)了,也沒有一個(gè)提示,顯得不是很友好,可以這樣

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('登錄超時(shí),請(qǐng)重新登錄');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);
      }
    }
  }
}

但是,假如是ajax請(qǐng)求呢?

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('登錄超時(shí),請(qǐng)重新登錄');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);
      }
    }
  }

以上所述是小編給大家介紹的Asp.net 中mvc 實(shí)現(xiàn)超時(shí)彈窗后跳轉(zhuǎn)功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論