Class AuthenticationBuilderExtensions
- Namespace
- Cuemon.Extensions.AspNetCore.Authentication
- Assembly
- Cuemon.Extensions.AspNetCore.Authentication.dll
Extension methods for the AuthenticationBuilder class.
public static class AuthenticationBuilderExtensions
- Inheritance
-
AuthenticationBuilderExtensions
Examples
The following example demonstrates how to register the Basic, Digest, and HMAC authentication handlers through the extension methods on .
using System;
using System.Security.Claims;
using Cuemon.AspNetCore.Authentication.Basic;
using Cuemon.AspNetCore.Authentication.Digest;
using Cuemon.AspNetCore.Authentication.Hmac;
using Cuemon.Extensions.AspNetCore.Authentication;
using Microsoft.Extensions.DependencyInjection;
namespace MyApp.Examples;
public static class AuthenticationBuilderExtensionsExample
{
public static void Demonstrate()
{
var services = new ServiceCollection();
services.AddLogging();
services.AddInMemoryDigestAuthenticationNonceTracker();
services.AddAuthentication(BasicAuthorizationHeader.Scheme)
.AddBasic(o =>
{
o.Authenticator = (username, password) => new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, username) }, BasicAuthorizationHeader.Scheme));
o.RequireSecureConnection = false;
})
.AddDigestAccess(o =>
{
o.Authenticator = (string username, out string password) =>
{
password = "Test";
return new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, username) }, DigestAuthorizationHeader.Scheme));
};
o.RequireSecureConnection = false;
})
.AddHmac(o =>
{
o.AuthenticationScheme = "hmac-docs";
o.Authenticator = (string clientId, out string clientSecret) =>
{
clientSecret = "Test";
return new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, clientId) }, o.AuthenticationScheme));
};
o.RequireSecureConnection = false;
});
using var provider = services.BuildServiceProvider();
Console.WriteLine(provider.GetRequiredService<BasicAuthenticationHandler>().GetType().Name);
Console.WriteLine(provider.GetRequiredService<DigestAuthenticationHandler>().GetType().Name);
Console.WriteLine(provider.GetRequiredService<HmacAuthenticationHandler>().GetType().Name);
}
}
Methods
AddBasic(AuthenticationBuilder, Action<BasicAuthenticationOptions>)
Adds an BasicAuthenticationHandler to the authentication middleware.
public static AuthenticationBuilder AddBasic(this AuthenticationBuilder builder, Action<BasicAuthenticationOptions> setup)
Parameters
builderAuthenticationBuilderThe AuthenticationBuilder to extend.
setupAction<BasicAuthenticationOptions>The BasicAuthenticationOptions which needs to be configured.
Returns
- AuthenticationBuilder
A reference to
builderso that additional calls can be chained.
Exceptions
- ArgumentNullException
buildercannot be null.- ArgumentException
setupfailed to configure an instance of BasicAuthenticationOptions in a valid state.
AddDigestAccess(AuthenticationBuilder, Action<DigestAuthenticationOptions>)
Adds an DigestAuthenticationHandler to the authentication middleware.
public static AuthenticationBuilder AddDigestAccess(this AuthenticationBuilder builder, Action<DigestAuthenticationOptions> setup)
Parameters
builderAuthenticationBuilderThe AuthenticationBuilder to extend.
setupAction<DigestAuthenticationOptions>The DigestAuthenticationOptions which needs to be configured.
Returns
- AuthenticationBuilder
A reference to
builderso that additional calls can be chained.
Exceptions
- ArgumentNullException
buildercannot be null.- ArgumentException
setupfailed to configure an instance of DigestAuthenticationOptions in a valid state.
AddHmac(AuthenticationBuilder, Action<HmacAuthenticationOptions>)
Adds an HmacAuthenticationHandler to the authentication middleware.
public static AuthenticationBuilder AddHmac(this AuthenticationBuilder builder, Action<HmacAuthenticationOptions> setup)
Parameters
builderAuthenticationBuilderThe AuthenticationBuilder to extend.
setupAction<HmacAuthenticationOptions>The HmacAuthenticationOptions which needs to be configured.
Returns
- AuthenticationBuilder
A reference to
builderso that additional calls can be chained.
Exceptions
- ArgumentNullException
buildercannot be null.- ArgumentException
setupfailed to configure an instance of HmacAuthenticationOptions in a valid state.