Asp.net 中mvc 实现超时弹窗后跳转功能
程序员文章站
2023-11-18 09:12:58
为了实现保持登录状态,可以用cookie来解决这一问题
假设过期时间为30分钟,校验发生在服务器,借助过滤器,可以这样写
public class power...
为了实现保持登录状态,可以用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 实现超时弹窗后跳转功能,希望对大家有所帮助