Dependency Injection on AuthorizationOptions Requirement in DotNet Core

前端 未结 2 1323
萌比男神i
萌比男神i 2021-02-04 11:37

I have a .NET core project and am trying to create a custom policy using AuthorizationOptions as shown in the documentation located here:

ASP.NET.Core Authorization - De

2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-04 12:28

    You can use also the GetRequiredService method:

    public class ExampleRequirement : AuthorizationHandler, IAuthorizationRequirement
    {
        protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, ExampleRequirement requirement)
        {
            UserManager UserManager = ((ActionContext)context.Resource).HttpContext.RequestServices.GetRequiredService>();
    
            // you can work with the users ...      
    
            return Task.CompletedTask;
        }
    }
    

提交回复
热议问题