Class AuthorizationResponseHandler
- Namespace
- Cuemon.Extensions.AspNetCore.Authentication
- Assembly
- Cuemon.Extensions.AspNetCore.Authentication.dll
Provides an opinionated implementation of IAuthorizationMiddlewareResultHandler that is optimized to deliver meaningful responses based on HTTP content negotiation.
public class AuthorizationResponseHandler : Configurable<AuthorizationResponseHandlerOptions>, IConfigurable<AuthorizationResponseHandlerOptions>, IAuthorizationMiddlewareResultHandler
- Inheritance
-
AuthorizationResponseHandler
- Implements
- Inherited Members
- Extension Methods
Examples
The following example demonstrates how to construct AuthorizationResponseHandler with configured options and the required logger dependency. It registers AuthorizationResponseHandlerOptions with FaultSensitivityDetails.All, adds logging services, and builds the service provider. The handler is then created from the resolved ILogger<T> and IOptions<T> instances, and its type name is written to the console, confirming the dependency-injection wiring works correctly.
using System;
using Cuemon.Extensions.AspNetCore.Authentication;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace MyApp.Examples;
public static class AuthorizationResponseHandlerExample
{
public static void Demonstrate()
{
var services = new ServiceCollection();
services.Configure<AuthorizationResponseHandlerOptions>(options =>
{
options.SensitivityDetails = Cuemon.Diagnostics.FaultSensitivityDetails.All;
});
services.AddLogging();
using var provider = services.BuildServiceProvider();
var logger = provider.GetRequiredService<ILogger<AuthorizationResponseHandler>>();
var options = provider.GetRequiredService<IOptions<AuthorizationResponseHandlerOptions>>();
var handler = new AuthorizationResponseHandler(logger, options);
Console.WriteLine(handler.GetType().Name);
}
}
Remarks
This implementation relies on IAuthenticateResultFeature to provide details about AuthN/AuthZ related issues.
Constructors
AuthorizationResponseHandler(ILogger<AuthorizationResponseHandler>, IOptions<AuthorizationResponseHandlerOptions>)
Initializes a new instance of the AuthorizationResponseHandler class.
public AuthorizationResponseHandler(ILogger<AuthorizationResponseHandler> logger, IOptions<AuthorizationResponseHandlerOptions> options)
Parameters
loggerILogger<AuthorizationResponseHandler>The dependency injected ILogger<TCategoryName>.
optionsIOptions<AuthorizationResponseHandlerOptions>The AuthorizationResponseHandlerOptions which may be configured.
Methods
HandleAsync(RequestDelegate, HttpContext, AuthorizationPolicy, PolicyAuthorizationResult)
Evaluates the authorization requirement and processes the authorization response.
public Task HandleAsync(RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationResult authorizeResult)
Parameters
nextRequestDelegateThe next middleware in the application pipeline.
contextHttpContextThe HttpContext of the current request.
policyAuthorizationPolicyThe AuthorizationPolicy for the resource.
authorizeResultPolicyAuthorizationResultThe PolicyAuthorizationResult of authorization.