Class BasicAuthenticationHandler
- Namespace
- Cuemon.AspNetCore.Authentication.Basic
- Assembly
- Cuemon.AspNetCore.Authentication.dll
Provides a HTTP Basic Authentication implementation of AuthenticationHandler<TOptions> for ASP.NET Core.
public class BasicAuthenticationHandler : AuthenticationHandler<BasicAuthenticationOptions>, IAuthenticationHandler
- Inheritance
-
BasicAuthenticationHandler
- Implements
- Inherited Members
Examples
The following example demonstrates how to register BasicAuthenticationHandler with ASP.NET Core authentication services. It sets up a ServiceCollection, registers the handler with a custom authenticator callback that validates credentials against hardcoded values, and builds the service provider. The handler is then resolved from DI and its type name is written to the console, confirming the authentication pipeline wires up correctly.
using System;
using System.Security.Claims;
using Cuemon.AspNetCore.Authentication.Basic;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.DependencyInjection;
namespace MyApp.Examples;
public static class BasicAuthenticationHandlerExample
{
public static void Demonstrate()
{
var services = new ServiceCollection();
services.AddLogging();
services.AddAuthentication(BasicAuthorizationHeader.Scheme)
.AddScheme<BasicAuthenticationOptions, BasicAuthenticationHandler>(BasicAuthorizationHeader.Scheme, options =>
{
options.Realm = "docs-example";
options.RequireSecureConnection = false;
options.Authenticator = (username, password) =>
{
if (username == "Agent" && password == "Test")
{
return new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, username) }, BasicAuthorizationHeader.Scheme));
}
return null;
};
});
using var provider = services.BuildServiceProvider();
var handler = provider.GetRequiredService<BasicAuthenticationHandler>();
Console.WriteLine(handler.GetType().Name);
}
}
Constructors
BasicAuthenticationHandler(IOptionsMonitor<BasicAuthenticationOptions>, ILoggerFactory, UrlEncoder)
Initializes a new instance of the BasicAuthenticationHandler class.
public BasicAuthenticationHandler(IOptionsMonitor<BasicAuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder)
Parameters
optionsIOptionsMonitor<BasicAuthenticationOptions>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.