Table of Contents

BasicAuthenticator Delegate

Definition

Namespace
Cuemon.AspNetCore.Authentication.Basic
Assemblies
Cuemon.AspNetCore.Authentication.dll
Source
src/Cuemon.AspNetCore.Authentication/Basic/BasicAuthenticator.cs

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

public delegate ClaimsPrincipal BasicAuthenticator(string username, string password)

Parameters

username string

The username that must be paired with password.

password string

The password that must be paired with username.

Returns

ClaimsPrincipal

A ClaimsPrincipal that is associated with the result of username and password.

Examples

The following example demonstrates how to assign and invoke a delegate when validating a basic-auth username and password pair.

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

namespace MyApp.Examples;

public static class BasicAuthenticatorExample
{
    public static void Demonstrate()
    {
        BasicAuthenticator authenticator = (username, password) =>
        {
            return username == "Agent" && password == "Test"
                ? new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, username) }, BasicAuthorizationHeader.Scheme))
                : null;
        };

        var principal = authenticator("Agent", "Test");

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