Class HmacAuthenticationHandler
- Namespace
- Cuemon.AspNetCore.Authentication.Hmac
- Assembly
- Cuemon.AspNetCore.Authentication.dll
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 type name 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(handler.GetType().Name);
}
}
Constructors
HmacAuthenticationHandler(IOptionsMonitor<HmacAuthenticationOptions>, ILoggerFactory, UrlEncoder)
Initializes a new instance of the HmacAuthenticationHandler class.
public HmacAuthenticationHandler(IOptionsMonitor<HmacAuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder)
Parameters
optionsIOptionsMonitor<HmacAuthenticationOptions>The monitor for the options instance.
loggerILoggerFactoryThe ILoggerFactory.
encoderUrlEncoderThe UrlEncoder.
Methods
HandleAuthenticateAsync()
Handle authenticate as an asynchronous operation.
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
Returns
- Task<AuthenticateResult>
A Task<TResult> representing the asynchronous operation.
HandleChallengeAsync(AuthenticationProperties)
Handle challenge as an asynchronous operation.
protected override Task HandleChallengeAsync(AuthenticationProperties properties)
Parameters
propertiesAuthenticationPropertiesThe properties.