全局过滤器:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6.  
  7. namespace UpdateService.Filter
  8. {
  9. public class AccessControl : ActionFilterAttribute
  10. {
  11. public override void OnActionExecuting(ActionExecutingContext filterContext)
  12. {
  13. bool flag = false;
  14. List<FilterAttribute> list = filterContext.ActionDescriptor.GetFilterAttributes(false).ToList();
  15. list.AddRange(filterContext.ActionDescriptor.ControllerDescriptor.GetFilterAttributes(false).ToList());
  16. foreach (var item in list)
  17. {
  18. bool b = item.Match(new AccessControlNoFilter());
  19. if (b == true)
  20. {
  21. flag = true;
  22. break;
  23. }
  24. }
  25. if (flag == true)
  26. {
  27. base.OnActionExecuting(filterContext);
  28. }
  29. else
  30. {
  31.  
  32. HttpContextBase context = filterContext.HttpContext;
  33. HttpResponseBase response = filterContext.HttpContext.Response;
  34. HttpRequestBase request = filterContext.HttpContext.Request;
  35. if (context.Session["User"] == null)
  36. {
  37. if (request.IsAjaxRequest())
  38. {
  39.  
  40. HttpUnauthorizedResult httpStatus = new HttpUnauthorizedResult("Session Timeout");
  41. filterContext.Result = httpStatus;
  42. //禁用web.config中的form认证
  43. //<!--<authentication mode="Forms">
  44. //<forms loginUrl="~/Account/Login" timeout="2880" />
  45. //</authentication>-->
  46. }
  47. else
  48. {
  49. ContentResult con = new ContentResult();
  50. con.Content = "<script>alert('当前会话已超时!');location='/Home/Login';</script>";
  51. filterContext.Result = con;
  52. }
  53. }
  54. base.OnActionExecuting(filterContext);
  55. }
  56.  
  57. }
  58. }
  59. }

NO全局过滤器:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6.  
  7. namespace UpdateService.Filter
  8. {
  9. public class AccessControlNoFilter : ActionFilterAttribute
  10. {
  11. public override void OnActionExecuting(ActionExecutingContext filterContext)
  12. {
  13. base.OnActionExecuting(filterContext);
  14. }
  15. }
  16. }

FilterConfig.cs

  1. public class FilterConfig
  2. {
  3. public static void RegisterGlobalFilters(GlobalFilterCollection filters)
  4. {
  5. //全局Session检查
  6. filters.Add(new AccessControl());
  7. }
  8. }

Jquery:

  1. $(function () {
  2. // 设置jQuery Ajax全局的参数
  3. $.ajaxSetup({
  4. complete: function (jqXHR, textStatus, errorThrown) {
  5. if (jqXHR.statusText == "Session Timeout") {
  6. alert('当前会话已超时!');
  7. location = "/Home/Login";
  8. }
  9.  
  10. }
  11. });
  12. });

Aop检查Session,全局过滤器和No全局过滤器的更多相关文章

  1. asp.net core MVC 全局过滤器之ExceptionFilter异常过滤器(一)

    本系类将会讲解asp.net core MVC中的内置全局过滤器的使用,将分为以下章节 asp.net core MVC 过滤器之ExceptionFilter异常过滤器(一) asp.net cor ...

  2. JNI/NDK开发指南(十)——JNI局部引用、全局引用和弱全局引用

    转自:http://blog.csdn.net/xyang81/article/details/44657385   这篇文章比较偏理论,详细介绍了在编写本地代码时三种引用的使用场景和注意事项.可能看 ...

  3. NDK开发之引用(局部引用,全局引用,虚全局引用)

    1.先引出我遇到的一个问题(我觉得先写问题,这样印象更深刻一点): Android Java层在调用本地jni代码的时候, 会维护一个局部引用表(最大长度是512), 一般jni函数调用结束后, jv ...

  4. ThinkPHP中:检查Session是否过期

    1.创建Session public function index(){ $sess_time=time(); session('name','andy'); session('time_stamp' ...

  5. rstful登陆认证并检查session是否过期

    一:restful用户视图 #!/usr/bin/env python # -*- coding:UTF-8 -*- # Author:Leslie-x from users import model ...

  6. Taurus.MVC WebAPI 入门开发教程6:全局控制器DefaultController与全局事件。

    系列目录 1.Taurus.MVC WebAPI  入门开发教程1:框架下载环境配置与运行. 2.Taurus.MVC WebAPI 入门开发教程2:添加控制器输出Hello World. 3.Tau ...

  7. 实现MVC自定义过滤器,自定义Area过滤器,自定义Controller,Action甚至是ViewData过滤器

    MVC开发中几种以AOP方式实现的Filters是非常好用的,默认情况下,我们通过App_Start中的FilterConfig来实现的过滤器注册是全局的,也就是整个应用程序都会使用的,针对单独的Fi ...

  8. MVC自定义过滤器,自定义Area过滤器,自定义Controller,Action甚至是ViewData过滤器

    实现MVC自定义过滤器,自定义Area过滤器,自定义Controller,Action甚至是ViewData过滤器 MVC开发中几种以AOP方式实现的Filters是非常好用的,默认情况下,我们通过A ...

  9. MVC过滤器:自定义授权过滤器

    一.授权过滤器 授权过滤器用于实现IAuthorizationFilter接口和做出关于是否执行操作方法(如执行身份验证或验证请求的属性)的安全策略.AuthorizeAttribute类继承了IAu ...

随机推荐

  1. 说说iDempiere = OSGi + ADempiere的OSGi

    怀揣着为中小企业量身定做一整套开源软件解决方案的梦想开始了一个网站的搭建.http://osssme.org/ 我对iDempiere还完全摸不着头脑,正好在学习之际,应erp100的@纵横四海 邀请 ...

  2. docker build 的 cache 机制

    cache 机制注意事项 可以说,cache 机制很大程度上做到了镜像的复用,降低存储空间的同时,还大大缩短了构建时间.然而,不得不说的是,想要用好 cache 机制,那就必须了解利用 cache 机 ...

  3. LaTeX数学公式输入

    [置顶 Tips ] 在 WinEdt 中快速添加公式字符而不必手动打出一个个letters~: 即会出现如下 GUI Page Control : ------------------------- ...

  4. jquery 设置checkbox选中 和获取选中值

    经常用到经常网上搜,这次写下来. 1,设置选中: $('#nrowid').prop('checked', false); 2,取选中项的值: $('#nrowid').prop("chec ...

  5. base、this、new、override、abstract、virtual、static

    前言 本文主要来讲解一下C#中,自己觉得掌握的不怎么样或者用的不多,不太熟悉的关键字,主要包括base.this.new.override.abstract.virtual以及针对static字段和s ...

  6. OSI7层网络模型

    物理层在OSI参考模型中,物理层(Physical Layer)是参考模型的最低层,也是OSI模型的第一层.物理层的主要功能是:利用传输介质为数据链路层提供物理连接,实现比特流的透明传输.物理层的作用 ...

  7. 微信模板消息php

    微信的模板消息需要认证的公众号后台申请 申请通过后就可以用平台定义的消息模板了 define('IN_ECS', true); require(dirname(__FILE__) . '/includ ...

  8. 设置phpcms v9黄页模块作为首页方法

    如果我们根据需要,想把黄页作为单独的网站,我们可以用模块化安装,并且首页设置,那么仿站网就说说详细的步骤.首先,我们需要安装最新版本的phpcms V9其次,下载黄页模块,然后进行根目录的替换.再次, ...

  9. The Way to Go读书笔记_第4章_基本结构和基本数据类型

    “_”标识符 _ 本身就是一个特殊的标识符,被称为空白标识符.它可以像其他标识符那样用于变量的声明或赋值(任何类型都可以赋值给它),但任何赋给这个标识符的值都将被抛弃,因此这些值不能在后续的代码中使用 ...

  10. std::thread “terminate called without an active exception”

    最近在使用std::thread的时候,遇到这样一个问题: std::thread t(func); 如果不使用调用t.join()就会遇到 "terminate called whitho ...