更新记录
转载请注明出处:
2022年11月24日 发布。
2022年11月20日 从笔记迁移到博客。
过滤器(Filter)基础
过滤器说明
过滤器与中间件很相似,过滤器(Filters)可在管道(pipeline)特定阶段(particular stage)前后执行操作。可以将过滤器视为拦截器(interceptors)。
过滤器级别范围(Scope of filters)
过滤器有多个级别,分别是:
-
全局级别过滤器(Global scope)
This means that the filter covers the whole MVC pipeline. Every call to a specific MVC route will pass through that filter.
-
控制器级别过滤器(Controller scope)
In this case, the filter is initialized as an attribute in one or multiple controller classes. It will act only on requests that have been directed to the target controllers.
-
动作级别过滤器(Action scope)
The filter is initialized as an attribute in one or multiple action methods. It will act only on requests that have been directed to the target methods.
预定义过滤器的类型(Out-of-the-box Filter Types)
There are different kinds of filters, and each filter is executed at a different stage in the pipeline. For example, action filters are executed when the action method is executed
Filter type | Type description |
---|---|
Authorization(授权) | This kind of filter is related to the authorization of users. It is the first filter that's executed in the filter pipeline and can short-circuit the pipeline of requests. |
Resource(资源) | Resource filters run immediately after authorization filters and after the rest of the pipeline has completed. They're useful when we wish to implement caching or for performance implementations. |
Page(页面) | 这种类型的过滤器用于在 Razor Pages 处理程序方法接收到请求之前修改请求,或者在生成操作结果之后修改操作结果。这种类型的过滤器只能应用于Razor Pages |
Action(操作) | Action filters are focused on the life cycle of action methods. They intercept and change the arguments and the returning results of action methods.仅在MVC中起作用。 |
Exception(异常) | Exception filters are used to intercept and apply cross-cutting policies to unhandled exceptions. |
Result(结果) | Result filters are executed immediately before and after the execution of an action result. They are usually implemented to change the formatting of the outcome. It is essential to note that they are executed only when the action method is completed successfully. |
不同类型的过滤器在ASP.NET Core中的位置。可以看到不同类型的过滤器在不同阶段起作用。授权过滤器先于其他所有操作,并在任何授权错误时阻止请求。 资源过滤器在模型验证和模型绑定请求之前运行,也在我们的请求结果从服务器返回时运行。 动作过滤器类型在动作调用之前和之后起作用。 此外,如果操作引发异常,则会触发异常过滤器。 在管道的末尾,结果过滤器对 IActionResult 最终对象实例进行操作。
Authorization filters
最先执行,用于判断用户是否授权。如果未授权,则直接结束当前请求。这种类型的过滤器实现了 IAsyncAuthorizationFilter 或IAuthorizationFilter 接口。
Resource filters
在Authorization过滤器后执行,并在执行其他过滤器 (除Authorization过滤器外)之前和之后执行。由于它在Action之前执行,因而可以用来对请求判断,根据条件来决定是否继续执行Action。这种类型过滤器实现了 IAsyncResourceFilter 或 IResourceFilter 接口。These are the filters that handle the request after authorization and are the last ones to handle the request before it leaves the filter pipeline. They are used to implement caching or by passing the filter pipeline。
Action filters
在Action执行的前后执行。与Resource过滤器不一样,它在模型绑定后执行。这种类型的过滤器实现了 IAsyncActionFilter 或 IActionFilter 接口。These wrap calls to individual action method calls and can manipulate the arguments passed in the action as well as the action result returned from it。
Exception filters
异常过滤器用于管理未处理的异常,比如:用于捕获异常。这种类型的过滤器实现了 IAsyncExceptionFilter 或 IExceptionFilter 接口。
Result filters
在 IActionResult 执行的前后执行,使用它能够控制Action的执行结果,比如:格式化结果等。需要注意的是,它只有在Action方法成功执行完成后才会运行。这种类型过滤器实现了 IAsyncResultFilter 或 IResultFilter 接口。
过滤器预定义接口
过滤器接口都实现了IFilterMetadata接口,它在Microsoft.AspNetCore.Mvc.Filters名称空间中。该接口是空的,并且不需要过滤器来实现任何特定行为。因为每个过滤器类别都以不同的方式工作。
namespace Microsoft.AspNetCore.Mvc.Filters
{
public interface IFilterMetadata { }
}
预定义的接口:
过滤器类型 | 接口 | 对应特性 |
---|---|---|
授权过滤器 | IAuthorizationFilter、IAsyncAuthorizationFilter | 没有提供特性类 |
资源过滤器 | IResourceFilter、IAsyncResourceFilter | 没有提供特性类 |
操作过滤器 | IActionFilter、IAsyncActionFilter | ActionFilterAttribute |
页面过滤器 | IPageFilter、IAsyncPageFilter | 没有提供特性类 |
结果过滤器 | IResultFilter、IAsyncResultFilter、 IAlwaysRunResultFilter、IAsyncAlwaysRunResultFilter |
ResultFilterAttribute |
异常过滤器 | IExceptionFilter、IAsyncExceptionFilter | ExceptionFilterAttribute |
授权过滤器(Authorization Filter)
说明
授权过滤器用于实现应用程序的安全策略。授权过滤器在其他类型的过滤器和端点处理请求之前执行,可以看前面的图片。
用IAuthorizationFilter接口实现授权过滤,定义如下:
namespace Microsoft.AspNetCore.Mvc.Filters {
public interface IAuthorizationFilter : IFilterMetadata
{
//调用 OnAuthorization 方法,实现授权过滤
void OnAuthorization(AuthorizationFilterContext context);
}
}
对应的异步版本,IAsyncAu