I am building a new Web API 2 based RESTful API. (I have to target the full framework due to some dependencies in the solution).
I have in the past used System.Security.Claims.ClaimAuthorizationManager to build in custom user security checks. This solution however is not very test friendly (DI) or async enabled.
I would like to use the Policy-based authorization available in aspnet core (https://learn.microsoft.com/en-us/aspnet/core/security/authorization/policies) as this seems to be a great model that fit my needs perfectly.
I am however stuck as to whether or not this is actually possible for not. It seems that the real technical sticking point may be that the policy requirement finds the requirement handler through registration of the handler in the services collection, for example:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddAuthorization(options =>
{
options.AddPolicy("AtLeast21", policy =>
policy.Requirements.Add(new MinimumAgeRequirement(21)));
});
services.AddSingleton<IAuthorizationHandler, MinimumAgeHandler>();
}
Is there any way to get the requirement handler to be picked up by the authorization service in a Web API 2 service?