Table of Contents

HmacAuthenticationHandler Class

Definition

Provides a HTTP HMAC Authentication implementation of AuthenticationHandler<TOptions> for ASP.NET Core.

public class HmacAuthenticationHandler : AuthenticationHandler<HmacAuthenticationOptions>, IAuthenticationHandler
Inheritance
HmacAuthenticationHandler
Implements
Inherited Members

Examples

The following example demonstrates how to register HmacAuthenticationHandler with ASP.NET Core authentication services. It creates a ServiceCollection, registers the handler under a custom HMAC scheme with a client ID/secret authenticator callback, and builds the service provider. The handler is resolved from DI and its configured scheme is written to the console, confirming that HMAC authentication configuration works end-to-end.

using System;
using System.Security.Claims;
using Cuemon.AspNetCore.Authentication.Hmac;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.DependencyInjection;

namespace MyApp.Examples;

public static class HmacAuthenticationHandlerExample
{
    public static void Demonstrate()
    {
        const string authenticationScheme = "hmac-docs";
        var services = new ServiceCollection();

        services.AddLogging();
        services.AddAuthentication(authenticationScheme)
            .AddScheme<HmacAuthenticationOptions, HmacAuthenticationHandler>(authenticationScheme, options =>
            {
                options.AuthenticationScheme = authenticationScheme;
                options.RequireSecureConnection = false;
                options.Authenticator = (string clientId, out string clientSecret) =>
                {
                    clientSecret = clientId == "Agent-Api" ? "Test" : null;
                    return clientSecret == null
                        ? null
                        : new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, clientId) }, authenticationScheme));
                };
            });

        using var provider = services.BuildServiceProvider();
        var handler = provider.GetRequiredService<HmacAuthenticationHandler>();

        Console.WriteLine($"Authentication scheme: {handler.Options.AuthenticationScheme}");
    }
}

Constructors

Name Description
HmacAuthenticationHandler(IOptionsMonitor<HmacAuthenticationOptions>, ILoggerFactory, UrlEncoder)

Initializes a new instance of the HmacAuthenticationHandler class.

Methods

Name Description
HandleAuthenticateAsync()

Handle authenticate as an asynchronous operation.

HandleChallengeAsync(AuthenticationProperties)

Handle challenge as an asynchronous operation.

See Also