国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

Asp.net 中mvc 實現超時彈窗后跳轉功能

2024-07-10 13:32:01
字體:
來源:轉載
供稿:網友

為了實現保持登錄狀態,可以用cookie來解決這一問題

假設過期時間為30分鐘,校驗發生在服務器,借助過濾器,可以這樣寫

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

但是頁面直接跳轉了,也沒有一個提示,顯得不是很友好,可以這樣

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('登錄超時,請重新登錄');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請求呢?

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('登錄超時,請重新登錄');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 實現超時彈窗后跳轉功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!


注:相關教程知識閱讀請移步到ASP.NET教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 灵宝市| 确山县| 潼南县| 塔城市| 长寿区| 资阳市| 图们市| 盖州市| 双峰县| 会泽县| 伊通| 遂平县| 庆元县| 安乡县| 鹿邑县| 长垣县| 确山县| 大关县| 拜泉县| 瑞丽市| 海原县| 桃源县| 页游| 缙云县| 平南县| 泽库县| 汽车| 鹤岗市| 东丽区| 长岭县| 肇源县| 突泉县| 安平县| 巩留县| 罗甸县| 宁安市| 石家庄市| 祁门县| 纳雍县| 顺平县| 黑河市|