为了实现保持登录状态,可以用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. Sublime的插件Color Highlighter的安装方法

    ColorHighlighter是一个显示选中颜色代码的视觉颜色的插件.如果您选择“# fff“,它将向您展示白色.ColorHighlighter支持所有CSS颜色格式,如Hex,RGB,HSL,H ...

  2. mysql中查看视图的元数据?

    需求描述: 查看视图的元数据的方法. 操作过程: 1.通过查看information_schema数据库下的views表来查看视图的定义语句 mysql> select definer,view ...

  3. Ajax请求全局配置

    摘要: jQuery已经成为项目中最常见的js库,也是前端开发最喜欢使用的库.下面是在项目中封装了jQuery的Ajax,分享给大家. 代码: // ajax 请求参数 var ajaxSetting ...

  4. Git------解决右键不显示Git Bash Here问题

    步骤: 1.通过在“运行”中输入‘regedit’,打开注册表. 2.找到[HKEY_CLASSES_ROOT\Directory\Background]. 3.在[shell]下右键-新建项[ope ...

  5. SpringMVC由浅入深day01_7入门程序小结

    7 入门程序小结 通过入门程序理解springmvc前端控制器.处理器映射器.处理器适配器.视图解析器用法. 前端控制器配置: 处理器映射器: 非注解处理器映射器(了解) 注解的处理器映射器(掌握) ...

  6. com.alibaba.fastjson.JSONObject

    package com.alibaba.fastjson; import java.util.Date; import java.util.List; import com.alibaba.fastj ...

  7. Hightchart 技巧

    http://blog.csdn.net/u014796515/article/details/24428131

  8. 【NGINX】Windows配置

    缺省安装nginx之后的配置 检查80端口占用 启动缺省配置之后的nginx 配置多端口站点 注册Server,server_name是配置的域名,proxy_pass是上图配置的代理地址 注意: 1 ...

  9. GreenPlum数据库安装

    第一章    文档概述 本文描述适用于Greenplum4.0以上版本的安装操作.所涉及到的操作系统相关参数调整,主要针对Redhat Linux操作系统. 第二章    安装介质 操作系统:Cent ...

  10. ISO27001信息安全管理体系

    0x00 前言 初入甲方,刚开始接触的应该就是ISO27001信息安全管理体系,你拿到的应该就是一整套安全管理类的文档.在甲方,稍微有点规模的公司很注重制度和流程,岗位职责分工明细,那么这些安全管理制 ...