AspNetCore.Docs/aspnetcore/security/authorization/dependencyinjection.md

93 lines
4.0 KiB
Markdown
Raw Permalink Normal View History

2016-10-29 01:35:15 +08:00
---
title: Dependency injection in requirement handlers in ASP.NET Core
2016-10-29 01:35:15 +08:00
author: rick-anderson
description: Learn how to inject authorization requirement handlers into an ASP.NET Core app using dependency injection.
monikerRange: ">= aspnetcore-2.1"
2018-01-29 23:21:31 +08:00
ms.author: riande
ms.date: 03/25/2022
2016-10-29 01:35:15 +08:00
uid: security/authorization/dependencyinjection
---
# Dependency injection in requirement handlers in ASP.NET Core
2016-10-29 01:35:15 +08:00
:::moniker range=">= aspnetcore-6.0"
2016-10-29 01:35:15 +08:00
[Authorization handlers must be registered](xref:security/authorization/policies#security-authorization-policies-based-handler-registration) in the service collection during configuration using [dependency injection](xref:fundamentals/dependency-injection).
2016-10-29 01:35:15 +08:00
2020-10-28 02:17:16 +08:00
Suppose you had a repository of rules you wanted to evaluate inside an authorization handler and that repository was registered in the service collection. Authorization resolves and injects that into the constructor.
2016-10-29 01:35:15 +08:00
For example, to use the .NET logging infrastructure, inject <xref:Microsoft.Extensions.Logging.ILoggerFactory> into the handler, as shown in the following example:
2016-10-29 01:35:15 +08:00
2016-11-18 13:03:07 +08:00
```csharp
public class SampleAuthorizationHandler : AuthorizationHandler<SampleRequirement>
{
private readonly ILogger _logger;
public SampleAuthorizationHandler(ILoggerFactory loggerFactory)
=> _logger = loggerFactory.CreateLogger(GetType().FullName);
protected override Task HandleRequirementAsync(
AuthorizationHandlerContext context, SampleRequirement requirement)
{
_logger.LogInformation("Inside my handler");
// ...
return Task.CompletedTask;
}
}
```
The preceding handler can be registered with any [service lifetime](/dotnet/core/extensions/dependency-injection#service-lifetimes). The following code uses <xref:Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton%2A> to register the preceding handler:
```csharp
builder.Services.AddSingleton<IAuthorizationHandler, SampleAuthorizationHandler>();
```
An instance of the handler is created when the app starts, and DI injects the registered `ILoggerFactory` into its constructor.
> [!NOTE]
> Don't register authorization handlers that use Entity Framework (EF) as singletons.
:::moniker-end
:::moniker range="< aspnetcore-6.0"
[Authorization handlers must be registered](xref:security/authorization/policies#security-authorization-policies-based-handler-registration) in the service collection during configuration using [dependency injection](xref:fundamentals/dependency-injection).
Suppose you had a repository of rules you wanted to evaluate inside an authorization handler and that repository was registered in the service collection. Authorization resolves and injects that into the constructor.
For example, to use the .NET logging infrastructure, inject <xref:Microsoft.Extensions.Logging.ILoggerFactory> into the handler, as shown in the following example:
2016-10-29 01:35:15 +08:00
2016-11-18 13:03:07 +08:00
```csharp
public class SampleAuthorizationHandler : AuthorizationHandler<SampleRequirement>
{
private readonly ILogger _logger;
public SampleAuthorizationHandler(ILoggerFactory loggerFactory)
=> _logger = loggerFactory.CreateLogger(GetType().FullName);
protected override Task HandleRequirementAsync(
AuthorizationHandlerContext context, SampleRequirement requirement)
{
_logger.LogInformation("Inside my handler");
// ...
return Task.CompletedTask;
}
}
```
The preceding handler can be registered with any [service lifetime](/dotnet/core/extensions/dependency-injection#service-lifetimes). The following code uses <xref:Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton%2A> to register the preceding handler:
```csharp
services.AddSingleton<IAuthorizationHandler, SampleAuthorizationHandler>();
```
2016-10-29 01:35:15 +08:00
An instance of the handler is created when the app starts, and DI injects the registered `ILoggerFactory` into its constructor.
2016-10-29 01:35:15 +08:00
> [!NOTE]
> Don't register authorization handlers that use Entity Framework (EF) as singletons.
:::moniker-end