您现在的位置是:网站首页> 编程资料编程资料
Asp.net 中mvc 实现超时弹窗后跳转功能_实用技巧_
2023-05-24
309人已围观
简介 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("","/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("","/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 实现超时弹窗后跳转功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
相关内容
- asp.net实现的MVC跨数据库多表联合动态条件查询功能示例_实用技巧_
- .NET实现WebSocket服务端即时通信实例_实用技巧_
- ASP.NET实现MVC中获取当前URL、controller及action的方法_实用技巧_
- ASP.NET MVC页面重定向简单介绍_实用技巧_
- .net 应对网站访问压力的方案总结_实用技巧_
- ASP.NET MVC分页控件_实用技巧_
- 详解ASP.NET提取多层嵌套json数据的方法_实用技巧_
- ASP.NET Forms身份认证_实用技巧_
- asp.net实现服务器文件下载到本地的方法_实用技巧_
- Asp.net MVC利用knockoutjs实现登陆并记录用户的内外网IP及所在城市(推荐)_实用技巧_
