Asp.net 中mvc 實現(xiàn)超時彈窗后跳轉(zhuǎn)功能
為了實現(xiàn)保持登錄狀態(tài),可以用cookie來解決這一問題
假設(shè)過期時間為30分鐘,校驗發(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)了,也沒有一個提示,顯得不是很友好,可以這樣
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 實現(xiàn)超時彈窗后跳轉(zhuǎn)功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
asp.net靜態(tài)方法彈出對話框?qū)崿F(xiàn)思路
為菜鳥所準(zhǔn)備……其實就是彈出JavaScript小窗口,總得來說就是定義的一個DIV,感興趣的朋友可以了解下,或許對你學(xué)習(xí)asp.net有所幫助2013-02-02VS2010/VS2013項目創(chuàng)建 ADO.NET連接mysql/sql server詳細(xì)步驟
這篇文章主要介紹了VS2010/VS2013項目創(chuàng)建,及ADO.NET連接mysql/sql server詳細(xì)步驟,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10Asp.net Core Jenkins Docker實現(xiàn)一鍵化部署的實現(xiàn)
這篇文章主要介紹了Asp.net Core Jenkins Docker實現(xiàn)一鍵化部署的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01asp.net 在處理向該請求提供服務(wù)所需的配置文件時出錯
遭遇:“說明: 在處理向該請求提供服務(wù)所需的配置文件時出錯。請檢查下面的特定錯誤詳細(xì)信息并適當(dāng)?shù)匦薷呐渲梦募!卞e誤2010-03-03如何傳值在2個頁面之間 要求不刷新父頁面,并且不能用Querystring傳值
通過Cookie,因為它既可以在服務(wù)器端對其進(jìn)行操作,也可在客戶端對其進(jìn)行操作但是缺點是不安全,而且有時客戶端會由于安全問題禁用Cookie!2008-12-12