.net mvc 权限验证 Filter(过滤器)
一、知识了解
- Asp.Net MVC提供了以下几种默认的Filter:

大家注意一点,Asp.Net MVC提供的ActionFilterAttribute默认实现了IActionFilter和IResultFilter。而ActionFilterAttribute是一个Abstract的类型,所以不能直接使用,因为它不能实例化,所以我们想使用它必须继承一下它然后才能使用。
Filter继承于ActionFilterAttribute抽象类,并可以覆写
void OnActionExecuting(ActionExecutingContext)
voidOnActionExecuted(ActionExecutedContext)
void OnResultExecuting(ResultExecutingContext)
void OnResultExecuted(ResultExecutedContext)。- 它们的执行先后顺序如下:
OnActionExecuting是Action执行前的操作
OnActionExecuted则是Action执行后的操作
OnResultExecuting是解析ActionResult前执行
OnResultExecuted是解析ActionResult后执行
接下来我们只要对以上的方法进行重写就可以在相应的步骤做一些操作了。
二、实操
1.首先添加一个普通的类TestFilterAttribute,这个类要继承ActionFilterAttribute,代码如下
/// <summary>
2 /// Filter 执行顺序Test,需要继承筛选器的基类ActionFilterAttribute
3 /// </summary>
4 public class TestFilterAttribute : ActionFilterAttribute
5 {
6 public string Message { get; set; }
7
8 public override void OnActionExecuting(ActionExecutingContext filterContext)
9 {
10 base.OnActionExecuting(filterContext);
11 filterContext.HttpContext.Response.Write("Action执行之前" + Message + "<br />");
12 }
13
14 public override void OnActionExecuted(ActionExecutedContext filterContext)
15 {
16 base.OnActionExecuted(filterContext);
17 filterContext.HttpContext.Response.Write("Action执行之后" + Message + "<br />");
18 }
19
20 public override void OnResultExecuting(ResultExecutingContext filterContext)
21 {
22 base.OnResultExecuting(filterContext);
23 filterContext.HttpContext.Response.Write("返回Result之前" + Message + "<br />");
24 }
25
26 public override void OnResultExecuted(ResultExecutedContext filterContext)
27 {
28 base.OnResultExecuted(filterContext);
29 filterContext.HttpContext.Response.Write("返回Result之后" + Message + "<br />");
30 }
31 }
写完这个代码后,我们回到Action上,打上上面的标记如下所示:
[TestFilterAttribute(Message = "Action")]
2 public ActionResult Index()
3 {
4 HttpContext.Response.Write("Action正在执行···<br />");
5 return Content("正在返回Result···<br />");
6 }
执行结果:

*如果我们将此标签打到Controller上的话,TestFilterAttributeFilter将作用到Controller下的所有的Action
*默认情况下Action上打了TestFilterAttribute 标签后,虽然在Controller上也打上了此标签,但它只有Action上的标签起作用了。
*补充:如果Action没有打上TestFilterAttribute标签,那么Controller上的标签便会被执行。
Index 执行时,Filter的方法只执行了一次,而某些情况下我们也想让Controller上的FilterAttribute也执行一次TestFilterAttribute,那我们怎么才能让Controller上的[TestFilter(Message = "controller")]也起作用呢?
答案是:我们只需在TestFilterAttribute类的定义上打上标记[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]即可,也就是让其成为可以多次执行的Action。代码如下:
/// <summary>
/// Filter 执行顺序Test,需要继承筛选器的基类ActionFilterAttribute
/// </summary
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)] //打标记是让所有的Controller的标记生效
public class TestFilterAttribute : ActionFilterAttribute
{
public string Message { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
filterContext.HttpContext.Response.Write("Action执行之前" + Message + "<br />");
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
filterContext.HttpContext.Response.Write("Action执行之后" + Message + "<br />");
}
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
base.OnResultExecuting(filterContext);
filterContext.HttpContext.Response.Write("返回Result之前" + Message + "<br />");
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
base.OnResultExecuted(filterContext);
filterContext.HttpContext.Response.Write("返回Result之后" + Message + "<br />");
}
}
效果:

问:如何注册到全局?
答案就在Global.asax中。让我们看以下代码,我是如何将上面我们定义的TestFilterAttribute 注册到全局Filter中
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
//注册全局过滤器
filters.Add(new TestFilterAttribute() { Message="全局"});
}
三、其他Fileter
1.AcceptVerbs 规定页面的访问形式,如
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Example(){
return View();
}
2.ActionName 如果不想用方法名做为Action名,或Action名为关键字的话,如
[ActionName("class")]
public ActionResult Example(){
return View();
}
3.NonAction 当前方法仅是普通方法不解析为Action
4.为Action添加缓存
[OutputCache(Duration = 60, VaryByParam = "*")]
public ActionResult Example()
{
return View();
}
5.Action可以接受Html等危险代码(ASP.NET MVC在aspx中设置<%@ Page 的属性无法完成等同任务。)
[ValidateInput(false)]
public ActionResult Example()
{
return View();
}
文章转载:https://www.cnblogs.com/zhangxiaoyong/p/6906288.html#autoid-0-0-0
.net mvc 权限验证 Filter(过滤器)的更多相关文章
- NET MVC权限验证
ASP.NET MVC权限验证 封装类 写该权限类主要目地 为了让权限配置更加的灵活,可以根据SQL.json.或者XML的方式来动态进行页面的访问控制,以及没有权限的相关跳转. 使用步骤 1.要建一 ...
- 关于filter web api mvc 权限验证 这里说的够详细了。。。
参考:http://www.cnblogs.com/willick/p/3331520.html Filter(筛选器)是基于AOP(面向方面编程)的设计,它的作用是对MVC框架处理客户端请求注入额外 ...
- MVC权限验证过滤器
Action属性,权限设定属性 [AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)] ...
- C# MVC权限验证
前言 之前一直没怎么接触过权限验证这块,刚好公司老平台改版,就有了这篇权限验证.此篇文章大致讲解下 精确到按钮级别的验证如何实现.以及权限验证设计的参考思路(菜鸟一枚,大神勿喷). 在开发大项目的时候 ...
- ASP.NET MVC权限验证 封装类
写该权限类主要目地 为了让权限配置更加的灵活,可以根据SQL.json.或者XML的方式来动态进行页面的访问控制,以及没有权限的相关跳转. 使用步骤 1.要建一个全局过滤器 //受权过滤器 publi ...
- .net web mvc 权限验证
这里分享MVC的权限验证,内容中可能存在一些,莫名其妙的方法,那些是以前封装好的,大致可以根据方法名称知道他的意思. using Game.Entity; using Game.Entity.Plat ...
- mvc 权限验证
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- mvc权限验证--AuthorizeAttribute
在做后台管理时用户登录后就需要验证哪些权限了,没有登录的就直接退出到登录页面. 系统有自带的权限[Authorize],可用于几个地方: 1.将属性[Authorize]置于相关的action上方,验 ...
- MVC权限验证之ActionFilterAttribute
参考:http://www.cnblogs.com/waitingfor/archive/2011/12/27/2303784.html ActionFilterAttribute是Action过滤类 ...
- Asp.net MVC 权限验证,以及是否允许匿名访问
public class CheckUserAttribute : ActionFilterAttribute, IAuthorizationFilter { public void OnAuthor ...
随机推荐
- pandas中loc和iloc的使用细节
1.缘由 前段时间在使用pandas库中的索引和切片的时候,突然就感觉有点懵,赋值和索引的操作总是报错. 网上的很多资料讲的也非常的浅显,而且使用起来非常不顺手. 于是我就找到很多的网上资料,然后自己 ...
- Django测试脚本-单表操作(增删改查)-必知必会13条-神奇的双下划线
目录 一:Django测试脚本 1.测试环境准备 2.tests.py 3.models.py 4.切换MySQL数据库 二:单表操作 1.pk关键字与get关键字 2.增 3.删 4.修 三:必知必 ...
- 第三模块的下载、requests模块、openpyxl模块
目录 第三方模块的下载安装 下载第三模块的方式 针对下载第三模块时可能会出现的问题 网络爬虫模块之requests模块 自动化办公领域之openpyxl模块 第三方模块的下载安装 第三方模块:别人写的 ...
- TCPView工具
TCPView:一个查看端口和线程的小工具.(不需安装) 主界面: 启动程序之后,你就发现TCPView将你目前在使用的所有进程都列举出来了,并时不时的会用红.黄.绿三种颜色标注某些进程: 红色代表该 ...
- excel甘特图制作
1.插入图表 1 1.选中数据区域(3列,如图所示)--点击插入--推荐的图表--堆积条形图 END 2.甘特图制作 1 2.点击图表工具--设计--选择数据. 轴标签区域改为--确定项目.. ...
- 精华推荐 |【深入浅出Sentinel原理及实战】「原理探索专题」完整剖析Alibaba微服务架构体系之轻量级高可用流量控制组件Sentinel(1)
Sentinel是什么?不要概念混淆啊! 注意:本Sentinel与Redis服务Sentinel是两回事,压根不是一个概念,请大家不要混肴. Alibaba的Sentinel Sentinel是由阿 ...
- css images图片铺满 不变型 以及头像裁剪 属性
一,图片的引入 background:url(img_flwr.gif); background-repeat:no-repeat; //平铺 二,图片的大小不不变形 background-size: ...
- 第二章 --------------------XAML基础
1.XAML是什么? XAML是扩展标记语言,是为了方便设计人员设计UI界面.具体关于XAML语法的讲解参考其他相关书籍. XAML每一个标签以<>开头,以</>结尾,作为标 ...
- 【转载】SQL SERVER 存储过程中执行动态Sql语句
MSSQL为我们提供了两种动态执行SQL语句的命令,分别是EXEC和sp_executesql;通常,sp_executesql则更具有优势,它提供了输入输出接口,而EXEC没有.还有一个最大的好处就 ...
- C#开发的磁吸屏幕类库 - 开源研究系列文章
上次写了一个关于线程池的博文,里面讲到了关于磁吸屏幕的类库,今天就把这个类库进行下讲解. 一. 类库目录: 类库的目录见下图,主要定义了Win32的一些API,以及一些API使用到的常量和结 ...