I am trying to create a new Authorization requirement, but it must use one of the services that I declare in ConfigureServices, and I have no idea how to pass that service to the new requirement in the same method as declaring the service.
public class NewRequirement: AuthorizationHandler<NewRequirement>, IAuthorizationRequirement
{
private IRepository _repository;
public NewRequirement(IRepository repository)
{
_repository = repository;
}
protected override void Handle(AuthorizationContext context, NewRequirement requirement)
{
//code that uses _repository here
}
}
This works fine, but when I try to add the policy in Startup.cs like this:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddSingleton<RepositoryContext>();
services.AddScoped<IRepository, Repository>();
services.Configure<AuthorizationOptions>(options =>
{
options.AddPolicy("NewRequirement", policy => policy.Requirements.Add(new NewRequirement()));
});
}
I get this error
Error CS7036
There is no argument given that corresponds to the required formal parameter 'repository' of 'ExtraProjectRequirements.NewRequirement(IRepository)'
Reception.DNX 4.5.1
I think I need to pass the IRepository to the new policy but I have no idea how to do it in ConfigureServices.