Asp.net 中mvc 实现超时弹窗后跳转功能
为了实现保持登录状态,可以用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); } } }
原文链接:http://www.cnblogs.com/cheesebar/archive/2017/02/10/6386479.html
(资源库 www.zyku.net)
栏 目:ASP.NET
本文地址:https://www.zyku.net/aspnet/527.html
您可能感兴趣的文章
- 03-25Win10 .Net Runtime Optimization Service占用大量CPU
- 03-21彻底解决Flash助手推荐弹窗广告(FF新推荐)
- 09-07jquery版右下角弹窗特效
- 04-17IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Inte
- 11-02无法向会话状态服务器发出会话状态请求。请确保 ASP.N
- 07-26asp.net使用ashx生成图形验证码的方法示例
- 07-18ASP.NET编程简单实现生成静态页面的方法
- 06-29ASP.NET中的参数与特殊类型和特性
- 06-29Asp.Net程序目录下文件夹或文件操作导致Session失效的
- 03-13纯js实现网站右下角弹窗实例
- 10-26华为ai音箱2怎样配对手机
- 10-27抖音未读消息怎么关闭
- 11-03抖音短视频如何关闭时间锁
- 01-11智能清理助手-智能清理助手应用软件功
- 10-30百度蜘蛛抓取耗时长解决方法
- 03-03WordPress实现每篇文章只允许用户评论
- 01-08致用生物-致用生物应用软件功能介绍
- 09-15支付宝一证通查入口分享
- 01-12运易安-运易安应用软件功能介绍
- 10-09vivox70pro+启用应用加密教程介绍
最近更新
阅读排行
猜你喜欢
- 01-19迷妹猜韩剧app-迷妹猜韩剧app应用软件
- 01-12Neets盒子-Neets盒子应用软件功能介绍
- 09-18小米手机怎么添加游戏加速到桌面
- 04-21小米11青春版开启来电感应方法
- 03-14vivoy53s设置导航键教程
- 03-13WordPress实现评论后可显示内容中附件
- 01-12掌上画画-掌上画画应用软件功能介绍
- 03-15荣耀50se隐藏应用步骤操作方法
- 01-12故事街道-故事街道应用软件功能介绍
- 01-11pop趋势网-pop趋势网应用软件功能介绍