全局过滤器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace UpdateService.Filter
{
public class AccessControl : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
bool flag = false;
List<FilterAttribute> list = filterContext.ActionDescriptor.GetFilterAttributes(false).ToList();
list.AddRange(filterContext.ActionDescriptor.ControllerDescriptor.GetFilterAttributes(false).ToList());
foreach (var item in list)
{
bool b = item.Match(new AccessControlNoFilter());
if (b == true)
{
flag = true;
break;
}
}
if (flag == true)
{
base.OnActionExecuting(filterContext);
}
else
{ HttpContextBase context = filterContext.HttpContext;
HttpResponseBase response = filterContext.HttpContext.Response;
HttpRequestBase request = filterContext.HttpContext.Request;
if (context.Session["User"] == null)
{
if (request.IsAjaxRequest())
{ HttpUnauthorizedResult httpStatus = new HttpUnauthorizedResult("Session Timeout");
filterContext.Result = httpStatus;
//禁用web.config中的form认证
//<!--<authentication mode="Forms">
//<forms loginUrl="~/Account/Login" timeout="2880" />
//</authentication>-->
}
else
{
ContentResult con = new ContentResult();
con.Content = "<script>alert('当前会话已超时!');location='/Home/Login';</script>";
filterContext.Result = con;
}
}
base.OnActionExecuting(filterContext);
} }
}
}

NO全局过滤器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace UpdateService.Filter
{
public class AccessControlNoFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
}
}
}

FilterConfig.cs

public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
//全局Session检查
filters.Add(new AccessControl());
}
}

Jquery:

$(function () {
// 设置jQuery Ajax全局的参数
$.ajaxSetup({
complete: function (jqXHR, textStatus, errorThrown) {
if (jqXHR.statusText == "Session Timeout") {
alert('当前会话已超时!');
location = "/Home/Login";
} }
});
});

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. iOS文件上传文件URL错误Invalid parameter not satisfying: fileURL'

    一:iOS文件上传提示URL错误 Invalid parameter not satisfying: fileURL' 二:解决方法: NSString *imagePath = [[NSBundle ...

  2. 7.12归来赛_B

    Prime Judge 时间限制 1000 ms 内存限制 65536 KB 题目描写叙述 众所周知.假设一个正整数仅仅能被1和自身整除,那么该数被称为素数.题目的任务非常easy.就是判定一个数是否 ...

  3. Android webView 支持缩放及自适应屏幕

    //支持javascript web.getSettings().setJavaScriptEnabled(true);  // 设置可以支持缩放  web.getSettings().setSupp ...

  4. resin设置jvm参数

    http://www.quiee.com.cn/archives/592/ resin resin3.0 及前期版本内存设置, 如下: 启动时设置虚拟内存: unix> bin/httpd.sh ...

  5. 解决在开发环境中访问json配置文件时报HTTP 错误 404.3 - Not Found

    问题描述: 在做demo时,使用json文件做配置文件,访问时,http状态提示:404.3错误,经验证json确认存在,路径也没有问题, 在浏览器地址栏里直接访问json时也提示同样的错误, 根据错 ...

  6. monogdb windows环境下 安装及使用简单示例

    1,下载地址:http://www.mongodb.org/downloads,选择windows平台,当前最新的版本是:2.6.4,本机是64位win7系统,我选择的是windows 64位msi, ...

  7. Specification模式的一个不错的示例代码

    using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace Specifi ...

  8. Android中的UriMatcher、ContentUrist和ContentResolver

    因为Uri代表了要操作的数据,所以我们很经常需要解析Uri,并从Uri中获取数据.Android系统提供了两个用于操作Uri的工具类,分别为UriMatcher 和ContentUris .掌握它们的 ...

  9. rabbitmq文章源

    网易杭研后台技术中心的博客 rabbitmq topic简单demo http://blog.csdn.net/cugb1004101218/article/details/21243927?utm_ ...

  10. Request介绍及演示样例 PART1

    Request在ServletAPI的规范连接地址http://blog.csdn.net/zghwaicsdn/article/details/51035146 HTTP简介 URL是浏览器寻找信息 ...