最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Asp.net 中mvc 实现超时弹窗后跳转功能
时间:2017-03-28 编辑:简简单单 来源:一聚教程网
为了实现保持登录状态,可以用cookie来解决这一问题
假设过期时间为30分钟,校验发生在服务器,借助过滤器,可以这样写
代码如下 | 复制代码 |
publicclassPowerFilter : AuthorizeAttribute { publicoverridevoidOnAuthorization(AuthorizationContext filterContext) { var cookie = HttpContext.Current.Request.Cookies["loginInfo"]; if(null== cookie) { filterContext.Result =newRedirectResult("/admin/login/index"); } else { cookie.Expires = DateTime.Now.AddMinutes(30); HttpContext.Current.Response.Cookies.Remove("loginInfo"); HttpContext.Current.Response.Cookies.Add(cookie); } } } |
但是页面直接跳转了,也没有一个提示,显得不是很友好,可以这样
代码如下 | 复制代码 |
publicclassPowerFilter : AuthorizeAttribute { publicoverridevoidOnAuthorization(AuthorizationContext filterContext) { var cookie = HttpContext.Current.Request.Cookies["loginInfo"]; if(null== cookie) { filterContext.Result =newContentResult() { Content =string .Format(" ","/admin/login/index") }; } else { cookie.Expires = DateTime.Now.AddMinutes(30); HttpContext.Current.Response.Cookies.Remove("loginInfo"); HttpContext.Current.Response.Cookies.Add(cookie); } } } } |
但是,假如是ajax请求呢?
代码如下 | 复制代码 |
publicclassPowerFilter : AuthorizeAttribute { publicoverridevoidOnAuthorization(AuthorizationContext filterContext) { var cookie = HttpContext.Current.Request.Cookies["loginInfo"]; if(null== cookie) { if(!filterContext.HttpContext.Request.IsAjaxRequest()) { filterContext.Result =newContentResult() { Content =string .Format(" ","/admin/login/index") }; } else { filterContext.Result =newJsonResult() { 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); } } } |
相关文章
- C#复制数组的两种方式及效率比较解读 10-24
- ASP.NET Identity用法解析 10-24
- ASP.NET MVC使用Identity增删改查用户介绍 10-24
- C语言中atoi函数模拟实现介绍 10-18
- .Net反向代理组件Yarp用法介绍 10-10
- .NET使用YARP通过编码方式配置域名转发实现反向代理教程 10-10