Table of Contents

Delegate HmacAuthenticator

Namespace
Cuemon.AspNetCore.Authentication.Hmac
Assembly
Cuemon.AspNetCore.Authentication.dll

Represents the method that defines an Authenticator typically assigned on HmacAuthenticationOptions.

public delegate ClaimsPrincipal HmacAuthenticator(string clientId, out string clientSecret)

Parameters

clientId string

The public key to match and lookup the paired shared clientSecret.

clientSecret string

The shared secret-private key paired with clientId.

Returns

ClaimsPrincipal

A ClaimsPrincipal that is associated with the result of clientSecret.

Examples

The following example demonstrates how to assign and invoke a delegate for client-id and shared-secret lookup.

using System;
using System.Security.Claims;
using Cuemon.AspNetCore.Authentication.Hmac;

namespace MyApp.Examples;

public static class HmacAuthenticatorExample
{
    public static void Demonstrate()
    {
        HmacAuthenticator 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) }, HmacFields.Scheme));
        };

        var principal = authenticator("Agent-Api", out var secret);

        Console.WriteLine(secret);
        Console.WriteLine(principal?.Identity?.Name);
    }
}