翻译如下:

  在配置期间(使用依赖注入),授权处理程序必须在服务集合中注册。

  假设您有一个在授权处理程序中要解析规则的仓储库,并且该仓储库已在服务集合中注册。 授权将在构造函数还原并注入。

  例如,如果你想使用ASP.NET的日志记录基础设施,你将ILoggerFactory注入你的处理程序。 这样的处理程序可能如下所示:

public class LoggingAuthorizationHandler : AuthorizationHandler<MyRequirement>
{
    ILogger _logger;

    public LoggingAuthorizationHandler(ILoggerFactory loggerFactory)
    {
        _logger = loggerFactory.CreateLogger(this.GetType().FullName);
    }

    protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, MyRequirement requirement)
    {
        _logger.LogInformation("Inside my handler");
        // Check if the requirement is fulfilled.
        return Task.CompletedTask;
    }
}

  您需要注册一个处理程序:services.AddSingleton():

services.AddSingleton<IAuthorizationHandler, LoggingAuthorizationHandler>();

  处理程序的实例将在应用程序启动时创建,我将注册的ILoggerFactory注入到构造函数中。

注意:

  使用Entity Framework的处理程序不应该注册为单例。

相关文章:

  • 2021-07-28
  • 2022-01-27
  • 2021-09-17
  • 2021-06-19
  • 2022-12-23
  • 2022-12-23
  • 2021-11-12
猜你喜欢
  • 2021-09-25
  • 2022-12-23
  • 2021-11-30
  • 2021-12-05
  • 2021-09-13
相关资源
相似解决方案