Aop检查Session,全局过滤器和No全局过滤器
全局过滤器:
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全局过滤器的更多相关文章
- asp.net core MVC 全局过滤器之ExceptionFilter异常过滤器(一)
本系类将会讲解asp.net core MVC中的内置全局过滤器的使用,将分为以下章节 asp.net core MVC 过滤器之ExceptionFilter异常过滤器(一) asp.net cor ...
- JNI/NDK开发指南(十)——JNI局部引用、全局引用和弱全局引用
转自:http://blog.csdn.net/xyang81/article/details/44657385 这篇文章比较偏理论,详细介绍了在编写本地代码时三种引用的使用场景和注意事项.可能看 ...
- NDK开发之引用(局部引用,全局引用,虚全局引用)
1.先引出我遇到的一个问题(我觉得先写问题,这样印象更深刻一点): Android Java层在调用本地jni代码的时候, 会维护一个局部引用表(最大长度是512), 一般jni函数调用结束后, jv ...
- ThinkPHP中:检查Session是否过期
1.创建Session public function index(){ $sess_time=time(); session('name','andy'); session('time_stamp' ...
- rstful登陆认证并检查session是否过期
一:restful用户视图 #!/usr/bin/env python # -*- coding:UTF-8 -*- # Author:Leslie-x from users import model ...
- Taurus.MVC WebAPI 入门开发教程6:全局控制器DefaultController与全局事件。
系列目录 1.Taurus.MVC WebAPI 入门开发教程1:框架下载环境配置与运行. 2.Taurus.MVC WebAPI 入门开发教程2:添加控制器输出Hello World. 3.Tau ...
- 实现MVC自定义过滤器,自定义Area过滤器,自定义Controller,Action甚至是ViewData过滤器
MVC开发中几种以AOP方式实现的Filters是非常好用的,默认情况下,我们通过App_Start中的FilterConfig来实现的过滤器注册是全局的,也就是整个应用程序都会使用的,针对单独的Fi ...
- MVC自定义过滤器,自定义Area过滤器,自定义Controller,Action甚至是ViewData过滤器
实现MVC自定义过滤器,自定义Area过滤器,自定义Controller,Action甚至是ViewData过滤器 MVC开发中几种以AOP方式实现的Filters是非常好用的,默认情况下,我们通过A ...
- MVC过滤器:自定义授权过滤器
一.授权过滤器 授权过滤器用于实现IAuthorizationFilter接口和做出关于是否执行操作方法(如执行身份验证或验证请求的属性)的安全策略.AuthorizeAttribute类继承了IAu ...
随机推荐
- Mysql 如何查询两个时间段之间的数据?
Mysql 如何查询两个时间段之间的数据?
- ORA-01589: 要打开数据库则必须使用 RESETLOGS 或 NORESETLOGS 选项
产生这个的原因可能是由于数据库突然停止,没有来得及将缓存区中的LOG归档,导致下次开启时不能匹配日志文件. 数据库中的三个日志文件挨个试,第二个就匹配上了
- wcf json asp.net json
function BindNewsTypeTree() { var parentid; // $.getJSON(serviceUrl + "NewsTypeService.svc/GetN ...
- JavaScript-常用正则函数(适合忘记时看)
test:测试string是否包含有匹配结果,包含返回true,不包含返回false. <script type="text/javascript"> var str ...
- tortoisesvn帮助手册
http://tortoisesvn.net/docs/nightly/TortoiseSVN_zh_CN/index.html
- crc32 根据字符串获取校验值
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text; n ...
- Application Request Route实现IIS Server Farms集群负载详解(转)
http://www.cnblogs.com/knowledgesea/p/5099893.html http://www.cnblogs.com/smileberry/p/4300849.html
- MySQL之mysql客户端工作的批处理一些使用手法
通常我们会用mysql这个客户端程序来连接mysql库.这个通常是工作在交互式模式下的.如我们连接上mysql并执行如下操作: mysql -uroot -h127. -P3306 Welcome t ...
- unity, particle play once and destroy
粒子播放一次后销毁: //ref: http://answers.unity3d.com/questions/219609/auto-destroying-particle-system ...
- C语言 fork
/* *@author cody *@date 2014-08-12 *@description */ /* #include <sys/types.h> #include <uni ...