Table of Contents

HmacAuthenticationMiddleware Class

Definition

Provides a HTTP HMAC Authentication middleware implementation for ASP.NET Core.

public class HmacAuthenticationMiddleware : ConfigurableMiddleware<HmacAuthenticationOptions>, IConfigurable<HmacAuthenticationOptions>
Inheritance
HmacAuthenticationMiddleware
Implements
Inherited Members

Examples

The following example demonstrates how to construct HmacAuthenticationMiddleware with inline option setup and invoke it against a signed DefaultHttpContext request.

// Program.cs
using System;
using System.Security.Claims;
using System.Threading.Tasks;
using Cuemon.AspNetCore.Authentication.Hmac;
using Microsoft.AspNetCore.Http;
using Microsoft.Net.Http.Headers;
using MyApp.Examples;

await HmacAuthenticationMiddlewareExample.DemonstrateAsync();

namespace MyApp.Examples
{
    public static class HmacAuthenticationMiddlewareExample
    {
        public static async Task DemonstrateAsync()
        {
            const string authenticationScheme = "hmac-docs";
            var middleware = new HmacAuthenticationMiddleware(_ => Task.CompletedTask, options =>
            {
                options.AuthenticationScheme = authenticationScheme;
                options.RequireSecureConnection = false;
                options.Authenticator = (string clientId, out string clientSecret) =>
                {
                    clientSecret = clientId == "Agent-Api" ? "Test" : string.Empty;
                    return clientId == "Agent-Api"
                        ? new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, clientId) }, authenticationScheme))
                        : null;
                };
            });

            var context = new DefaultHttpContext();
            context.Request.Method = HttpMethods.Get;
            context.Request.Scheme = "https";
            context.Request.Host = new HostString("www.cuemon.net");
            context.Request.Path = "/orders";
            context.Request.Headers[HeaderNames.Date] = DateTime.UtcNow.ToString("R");

            var token = new HmacAuthorizationHeaderBuilder(authenticationScheme)
                .AddFromRequest(context.Request)
                .AddClientId("Agent-Api")
                .AddClientSecret("Test")
                .AddCredentialScope("20150830/us-east-1/iam/aws4_request")
                .Build()
                .ToString();

            context.Request.Headers[HeaderNames.Authorization] = token;

            await middleware.InvokeAsync(context);

            Console.WriteLine($"Scheme: {middleware.Options.AuthenticationScheme}");
            Console.WriteLine($"Authenticated: {context.User.Identity?.IsAuthenticated}");
            Console.WriteLine($"Client: {context.User.Identity?.Name}");
        }
    }
}

Constructors

Name Description
HmacAuthenticationMiddleware(RequestDelegate, IOptions<HmacAuthenticationOptions>)

Initializes a new instance of the HmacAuthenticationMiddleware class.

HmacAuthenticationMiddleware(RequestDelegate, Action<HmacAuthenticationOptions>)

Initializes a new instance of the HmacAuthenticationMiddleware class.

Methods

Name Description
InvokeAsync(HttpContext)

Executes the HmacAuthenticationMiddleware.