myesn

myEsn2E9

hi
github

ASP.NET Core Filters 过滤器

  • Authorization Filters:身份验证和鉴权
  • Action Filters:执行 Action 方法前、后
  • Result Filters:执行 Action 方法并生成结果之后
  • Exception Filters:处理异常

全局

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc(config =>
    {
        var policy = new AuthorizationPolicyBuilder()
            .RequireAuthenticatedUser()
            .Build();
        config.Filters.Add(new AuthorizeFilter(policy));
    });
}

局部

可以加在 controller class 或者 action method 上。

[Authorize]
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.