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();
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();
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();
HttpContext.Current.Response.Cookies.Remove("loginInfo");
HttpContext.Current.Response.Cookies.Add(cookie);
}
}
}
mvc 实现超时弹窗后跳转的更多相关文章
- php弹窗后跳入另一个页面
之前写项目时,在跳转页面前加入一个弹窗,发现弹窗没有弹出来就直接跳转了,之前使用的header跳转发现不行,换成location.href也不行,后来再location.href前加入一个parent ...
- layer弹窗的跳转功能
1,本弹窗直接跳转父页面: @if(session('message')) <script> window.parent.location.reload(); //刷新父页面 var in ...
- 详解springmvc控制登录用户session失效后跳转登录页面
springmvc控制登录用户session失效后跳转登录页面,废话不多少了,具体如下: 第一步,配置 web.xml <session-config> <session-timeo ...
- swf反编辑软件带弹窗和跳转swf文件
http://www.wocaoseo.com/thread-296-1-1.html swf反编辑有啥用,在seo上.淘客上.网赚上,只有稍微牛逼些的人恐怕无人不知.无人不晓吧,这个软件是完全免费的 ...
- MVC遇上bootstrap后的ajax表单模型验证
MVC遇上bootstrap后的ajax表单验证 使用bootstrap后他由他自带的样式has-error,想要使用它就会比较麻烦,往常使用jqueyr.validate的话只有使用他自己的样式了, ...
- IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Integrated-4.0解决方法IIS上部署MVC网站,打开后500错误
IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Integrated-4.0解决方法 IIS上部署MVC网站,打开后500错误:处理程序“ExtensionlessUrl ...
- web.config中配置页面出错后跳转指定错误页面
每当用户访问错误页面时,会出现不友好的404错误,所以为了防止这种不友好,我们在web.config中的<system.web>节点下配置 <customErrors>,在出现 ...
- android 页面停几秒后跳转
<span style="white-space:pre"> </span>//实现等待几秒后跳转,方法一 new Handler().pos ...
- IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Integrated-4.0解决方法
IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Integrated-4.0解决方法 IIS上部署MVC网站,打开后500错误:处理程序“ExtensionlessUrl ...
随机推荐
- 【转】Redis集群搭建与简单使用
介绍安装环境与版本 用两台虚拟机模拟6个节点,一台机器3个节点,创建出3 master.3 salve 环境. redis 采用 redis-3.2.4 版本. 两台虚拟机都是 CentOS ,一台 ...
- 【转】Java中的多线程学习大总结
多线程作为Java中很重要的一个知识点,在此还是有必要总结一下的. 一.线程的生命周期及五种基本状态 关于Java中线程的生命周期,首先看一下下面这张较为经典的图: 上图中基本上囊括了Java中多线程 ...
- Linux的wget命令
wget是linux最常用的下载命令, 一般的使用方法是: wget + 空格 + 要下载文件的url路径 例如: # wget http://www.linuxsense.org/xxxx/xxx. ...
- IOS UILineBreakMode的各种情况分析
typedef enum { UILineBreakModeWordWrap = 0, UILineBreakModeCharacterWrap, UILineBreakModeCl ...
- Linux下修改当前用户的最大线程数和 open files
1 查看当前用户的线程 ulimit -a 2 修改配置文件 vi /etc/security/limits.d/90-nproc.conf 3 改完即可生效 4 修改可打开的最大文件数 vi /e ...
- Android ViewDragHelper全然解析 自己定义ViewGroup神器
转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/46858663. 本文出自:[张鸿洋的博客] 一.概述 在自己定义ViewGro ...
- C#(少用的)
挖一挖C#中那些我们不常用的东西之系列(1)——ToDictionary,ToLookup 挖一挖C#中那些我们不常用的东西之系列(2)——IsXXX 系列方法 挖一挖C#中那些我们不常用的东西之系列 ...
- mysql触发器的使用 想让b字段在更新的时候把旧数据保存到a字段中
使用mysql希望数据库自动触发一些规则,进行更新数据的时候,就需要用触发器了,比如 将旧数据保存到额外字段中,如何做呢? 在abc表中 name更新的时候 我希望把name的老数据保存到 old_n ...
- form enctype:"multipart/form-data",method:"post" 提交表单,后台获取不到数据
在解决博问node.js接受参数的时候,发现当form中添加enctype:"multipart/form-data",后台确实获取不到数据,于是跑到百度上查了一下,终于明白为什么 ...
- SpringBoot(十二)-- 整合Redis
1.pom依赖 <!-- 添加redis支持 --> <dependency> <groupId>org.springframework.boot</grou ...