为了实现保持登录状态,可以用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. IT部门的“2/8”现状

    专家的研究和大量企业实践表明,在IT项目的生命周期中,大约80%的时间与IT项目运 营维护有关,而该阶段的投资仅占整个IT投资的20%,形成了典型的“技术高消费”.“轻服务.重技术”现象.Gartne ...

  2. RESTEasy maven使用

    添加依赖: <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteas ...

  3. Java动态调用类中方法

    在Java中,调用类的方法有两种方式:对于静态方法可以直接使用类名调用,对于非静态方法必须使用类的对象调用.反射机制提供了比较另类的调用方式,可以根据需要指定要调用的方法,而不必在编程时确定.调用的方 ...

  4. How to solve the problem : &quot;You have been logged on with a temporary profile&quot;

    /*By Jiangong SUN*/ I've encountered a problem in one server, which is : Every time I login into the ...

  5. underscore.js定义模板遇到问题:Uncaught TypeError: Cannot read property 'replace' of undefined

    代码正确缩进位置如下, extend "layout" block 'content',-> div ->'nihao' script id:"Invoice ...

  6. 如何使用 SSH 连接 VMWare 虚拟机中的 Ubuntu

    环境:VMWare Player 5.0.2,Ubuntu 13.10  VMWare共有3种网络连接模式,分别是: 1. bridged(桥接模式):虚拟机将直接连接到物理局域网,使自身独立于宿主机 ...

  7. session会话保持

    #coding=utf-8 from flask import Flask from flask import request from flask import redirect from flas ...

  8. Servlet基本用法(二)接口和类

    一.摘要 本文主要简单介绍开发Servlet需要用到的接口和类. 二.ServletRequest和ServletResponse接口 当客户请求到来时,由容器创建一个ServletRequest对象 ...

  9. jquery on=>keyup无法绑定未来js添加的元素

    $("[name=red_count]").on("keyup", countKeyUpBind);即使使用on的,也无法绑定未来元素, 所以直接在动态添加的时 ...

  10. 树莓派3安装opencv2程序无法运行

    在raspberry pi3 上安装opencv3已测试,没有问题,而opencv2报错如下: Xlib: extension "RANDR" missing on display ...