为了实现保持登录状态,可以用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 实现超时弹窗后跳转的更多相关文章

  1. php弹窗后跳入另一个页面

    之前写项目时,在跳转页面前加入一个弹窗,发现弹窗没有弹出来就直接跳转了,之前使用的header跳转发现不行,换成location.href也不行,后来再location.href前加入一个parent ...

  2. layer弹窗的跳转功能

    1,本弹窗直接跳转父页面: @if(session('message')) <script> window.parent.location.reload(); //刷新父页面 var in ...

  3. 详解springmvc控制登录用户session失效后跳转登录页面

    springmvc控制登录用户session失效后跳转登录页面,废话不多少了,具体如下: 第一步,配置 web.xml <session-config> <session-timeo ...

  4. swf反编辑软件带弹窗和跳转swf文件

    http://www.wocaoseo.com/thread-296-1-1.html swf反编辑有啥用,在seo上.淘客上.网赚上,只有稍微牛逼些的人恐怕无人不知.无人不晓吧,这个软件是完全免费的 ...

  5. MVC遇上bootstrap后的ajax表单模型验证

    MVC遇上bootstrap后的ajax表单验证 使用bootstrap后他由他自带的样式has-error,想要使用它就会比较麻烦,往常使用jqueyr.validate的话只有使用他自己的样式了, ...

  6. IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Integrated-4.0解决方法IIS上部署MVC网站,打开后500错误

    IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Integrated-4.0解决方法 IIS上部署MVC网站,打开后500错误:处理程序“ExtensionlessUrl ...

  7. web.config中配置页面出错后跳转指定错误页面

    每当用户访问错误页面时,会出现不友好的404错误,所以为了防止这种不友好,我们在web.config中的<system.web>节点下配置 <customErrors>,在出现 ...

  8. android 页面停几秒后跳转

      <span style="white-space:pre">      </span>//实现等待几秒后跳转,方法一 new Handler().pos ...

  9. IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Integrated-4.0解决方法

    IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Integrated-4.0解决方法 IIS上部署MVC网站,打开后500错误:处理程序“ExtensionlessUrl ...

随机推荐

  1. 关于解决emoji表情的存储

    近段时间处理,由于工作需求,需要使得用户插入的emoji表情能够正常显示及使用,所以做个总结,以备后用. 说明:本方法只在mysql环境中测试 1.首先程序在连接数据库时,要指定数据库字符集的设置 c ...

  2. 【Winform】自定义Messagebox

    1.保持Msgbox的Icon 2.可以追加Checkbox,RadioBOx 下载

  3. Dubbo -- 系统学习 笔记 -- 示例 -- 结果缓存

    Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 结果缓存 结果缓存,用于加速热门数据的访问速度,Dubbo提供声明式缓存,以减少用 ...

  4. 如何在windows上测试iphone?

    本教程将会让你没有mac照样测试iphone,这是我折腾了几天总结下来的,希望对大家有用. 先来几张效果图吧 方法很简单,但是配置起来说实话有点麻烦,先在电脑上安装vmware,在安装osx系统,在安 ...

  5. C#中的Abstract、Virtual、Interface理解

    容易混淆是必须的,都是与继承有关系,并且涉及到override的使用 一.Virtual方法(虚方法) virtual 关键字用于在基类中修饰方法.virtual的使用会有两种情况: 情况1:在基类中 ...

  6. ios开发之--首页 导航栏隐藏 下一级页面显示,pop回来显示白条

    解决方法,在首页中实现如下两个方法,代码如下: -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated] ...

  7. linux下中文文件名乱码解决

    在windows上使用ftp上传文件到Linux上,中文名称在Linux系统中显示为乱码.虽然将Linux的env设置了LANG=en_US.UTF-8,并且本地的Shell客户端编码也设置成UTF- ...

  8. Nginx(十)-- 进程模型及工作原理

    1.nginx进程模型 Nginx是一个master和worker的模型.master主要用来管理worker进程,master就比作老板,worker就是打工仔,master指挥worker来做事情 ...

  9. Splash wait() 方法

    wait()方法用于控制页面的等待时间,如下,实现访问淘宝并等待2秒,随后返回淘宝页面的源代码: function main(splash) splash:go("https://www.t ...

  10. Windows 下 Tomcat 添加为系统服务

    标记一下,以便以后查看 setclasspath.bat 第一行插入 SET JAVA_HOME=C:\Program Files\Java\jre1.8.0_51 service.bat 第一行插入 ...