Table of Contents

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

logger ILogger<AuthorizationResponseHandler>

The dependency injected ILogger<TCategoryName>.

options IOptions<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

next RequestDelegate

The next middleware in the application pipeline.

context HttpContext

The HttpContext of the current request.

policy AuthorizationPolicy

The AuthorizationPolicy for the resource.

authorizeResult PolicyAuthorizationResult

The PolicyAuthorizationResult of authorization.

Returns

Task