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 ...
随机推荐
- Python 二维码解码
二维码解析 Python中关于二维码解析的现成模块有很多,比较著名的就是Zbar以及ZXing.然而很不幸的是,官方的版本都是支持到python2.x,下面是在python2.x的例子: import ...
- 反射与annotation
1,可以通过反射取得使用的全部annotation 2,可以通过反射取得指定的annotation. 一个annotation要想变得有意义, 必须结合反射机制取得annotation中设置的全部内容 ...
- init: cannot execve(‘XXX’):Permission denied问题
近期在给android 4.3系统进行root时候,一直出现例如以下的红色权限问题 : 7.695741] Freeing init memory: 236K [ 8.555286] ini ...
- SqlServer报错:System.Data.SqlClient.SqlException
在将Asp.Net MVC4项目部署到新机器上进行调试的时候,出现了如下错误: System.Data.SqlClient.SqlException 具体的内容如下: System.Data.SqlC ...
- android 中的 Handler 线程间通信
一. 在MainActivity中为什么只是类似的写一行如下代码就可以使用handler了呢? Handler handler = new Handler() { @Override public v ...
- unity, TRANSFORM_TEX
TRANSFORM_TEX在UnityCG.cginc中定义. ----补充: 为啥buildin shader Unlit-Normal.shader中有一个float4 _MainTex_ST变 ...
- 怎样优化UITableView的性能
在iOS App中,UITableView应该是使用率最高的.同一时候也是最为复杂的视图. 差点儿全部自带的应用中都能看到它的身影,可见它的重要性. 在使用UITableView时,会常常遇到性能上的 ...
- NGUI 取ScrollView中遮罩区域4个点
用panel.localCorners而不是panel.finalClipRegion,Region还要再换算 首先通过ScrollView取panel,然后取Corners,它返回值代表4个点,映射 ...
- [elk]logstash&filebeat常用语句
filebeat安装dashboard 参考: https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-getting-star ...
- PHP系统学习3 正则
正则 ^shop 标示匹配与shop开头的字符串 shop$用来匹配与shop结尾的字符串 ^shop$只匹配shop [a-z]匹配所有小写字母 [A-Z]匹配所有大写字母 [a-zA-Z]匹配所有 ...