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

