如何在一个区域内的所有控制器上强制授权?具体来说,我想配置一个AuthorizeFilter
,作为Startup.ConfigureServices()
方法的一部分应用于“ admin”区域。
答案 0 :(得分:3)
一种方法是使用自定义IControllerModelConvention
public class AuthorizeAreaConvention : IControllerModelConvention
{
private readonly string _area;
private readonly string _policy;
public AuthorizeAreaConvention(string area, string policy)
{
_area = area;
_policy = policy;
}
public void Apply(ControllerModel controller)
{
if (controller.Attributes.Any(a =>
a is AreaAttribute && (a as AreaAttribute).RouteValue.Equals(_area, StringComparison.OrdinalIgnoreCase))
|| controller.RouteValues.Any(r =>
r.Key.Equals("area", StringComparison.OrdinalIgnoreCase) && r.Value.Equals(_area, StringComparison.OrdinalIgnoreCase)))
{
controller.Filters.Add(new AuthorizeFilter(_policy));
}
}
}
然后在您的Startup.ConfigureServices()
方法中,可以添加:
services.AddMvc(options =>
{
options.Conventions.Add(
new AuthorizeAreaConvention("Admin", Policy.AdministratorPolicy));
});
Policy.AdministratorPolicy
只是一个字符串常量(例如"AdministratorPolicy"
),它在应用程序StartUp
中的注册方式如下:
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthorization(o =>
{
o.AddPolicy(Policy.AdministratorPolicy, b =>
{
b.RequireAuthenticatedUser();
b.RequireClaim(ClaimTypes.Role, Roles.Admin);
});
});
// Add framework services.
services.AddMvc();
// ... Omitted for brevity
}
在上面,Roles.Admin
只是一个字符串常量(例如"Admin"
)。