Asp.net 中mvc 實(shí)現(xiàn)超時(shí)彈窗后跳轉(zhuǎn)功能
為了實(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)文章
asp.net靜態(tài)方法彈出對(duì)話框?qū)崿F(xiàn)思路
為菜鳥所準(zhǔn)備……其實(shí)就是彈出JavaScript小窗口,總得來說就是定義的一個(gè)DIV,感興趣的朋友可以了解下,或許對(duì)你學(xué)習(xí)asp.net有所幫助2013-02-02VS2010/VS2013項(xiàng)目創(chuàng)建 ADO.NET連接mysql/sql server詳細(xì)步驟
這篇文章主要介紹了VS2010/VS2013項(xiàng)目創(chuàng)建,及ADO.NET連接mysql/sql server詳細(xì)步驟,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Asp.net Core Jenkins Docker實(shí)現(xiàn)一鍵化部署的實(shí)現(xiàn)
這篇文章主要介紹了Asp.net Core Jenkins Docker實(shí)現(xiàn)一鍵化部署的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01C#中的cookie編程簡(jiǎn)單實(shí)例與說明
這篇文章介紹了C#中的cookie編程簡(jiǎn)單實(shí)例與說明,有需要的朋友可以參考一下2013-07-07asp.net 在處理向該請(qǐng)求提供服務(wù)所需的配置文件時(shí)出錯(cuò)
遭遇:“說明: 在處理向該請(qǐng)求提供服務(wù)所需的配置文件時(shí)出錯(cuò)。請(qǐng)檢查下面的特定錯(cuò)誤詳細(xì)信息并適當(dāng)?shù)匦薷呐渲梦募??!卞e(cuò)誤2010-03-03asp.net(C#)遍歷memcached緩存對(duì)象
出于性能考慮,memcached沒有提供遍歷功能,不過我們可以通過以下兩個(gè)stats命令得到所有的緩存對(duì)象。2010-03-03如何傳值在2個(gè)頁面之間 要求不刷新父頁面,并且不能用Querystring傳值
通過Cookie,因?yàn)樗瓤梢栽诜?wù)器端對(duì)其進(jìn)行操作,也可在客戶端對(duì)其進(jìn)行操作但是缺點(diǎn)是不安全,而且有時(shí)客戶端會(huì)由于安全問題禁用Cookie!2008-12-12