Table of Contents

Class Authenticator

Namespace
Cuemon.AspNetCore.Authentication
Assembly
Cuemon.AspNetCore.Authentication.dll

Provides a set of static methods for working with HTTP based authentication.

public static class Authenticator
Inheritance
Authenticator

Examples

The following example demonstrates how to authenticate an HTTP request by parsing the Authorization header and resolving a claims principal.

using System;
using System.Security.Claims;
using Cuemon;
using Cuemon.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.Net.Http.Headers;

namespace MyApp.Examples;

public static class AuthenticatorExample
{
    public static void Demonstrate()
    {
        var context = new DefaultHttpContext()
        {
            Request = { IsHttps = true }
        };
        context.Request.Headers.Append(HeaderNames.Authorization, "Basic YWxpY2U6cGFzc3dvcmQ=");

        var result = Authenticator.Authenticate<string>(context, false,
            (HttpContext _, string authorizationHeader) => authorizationHeader,
            (HttpContext _, string credentials, out ConditionalValue<ClaimsPrincipal> principal) =>
            {
                principal = new SuccessfulValue<ClaimsPrincipal>(
                    new ClaimsPrincipal(new ClaimsIdentity(
                        new[] { new Claim(ClaimTypes.Name, "alice") }, "Basic")));
                return true;
            });

        Console.WriteLine("Succeeded: " + result.Succeeded);
        Console.WriteLine("User: " + result.Result?.Identity?.Name);
    }
}

Methods

Authenticate<T>(HttpContext, bool, Func<HttpContext, string, T>, TesterFunc<HttpContext, T, ConditionalValue<ClaimsPrincipal>, bool>)

Provides a generic way to make authentication requests using the specified context.

public static ConditionalValue<ClaimsPrincipal> Authenticate<T>(HttpContext context, bool requireSecureConnection, Func<HttpContext, string, T> authorizationParser, TesterFunc<HttpContext, T, ConditionalValue<ClaimsPrincipal>, bool> principalParser)

Parameters

context HttpContext

The context of the ASP.NET application.

requireSecureConnection bool

When true, the HTTP connection is required to use secure sockets (that is, HTTPS); when false no requirement is enforced.

authorizationParser Func<HttpContext, string, T>

The function delegate that will parse the authorization header of a web request and return the credentials of T.

principalParser TesterFunc<HttpContext, T, ConditionalValue<ClaimsPrincipal>, bool>

The function delegate that will parse the credentials of T returned from authorizationParser and if successful returns a ClaimsPrincipal object.

Returns

ConditionalValue<ClaimsPrincipal>

A ClaimsPrincipal if principalParser was successful.

Type Parameters

T

The type of the credentials returned from authorizationParser and passed to principalParser.

Exceptions

SecurityException

Authorized failed for the request.

TryAuthenticate<T>(HttpContext, bool, Func<HttpContext, string, T>, TesterFunc<HttpContext, T, ConditionalValue<ClaimsPrincipal>, bool>, out ConditionalValue<ClaimsPrincipal>)

Provides a generic way to make authentication requests using the specified context.

public static bool TryAuthenticate<T>(HttpContext context, bool requireSecureConnection, Func<HttpContext, string, T> authorizationParser, TesterFunc<HttpContext, T, ConditionalValue<ClaimsPrincipal>, bool> principalParser, out ConditionalValue<ClaimsPrincipal> principal)

Parameters

context HttpContext

The context of the ASP.NET application.

requireSecureConnection bool

When true, the HTTP connection is required to use secure sockets (that is, HTTPS); when false no requirement is enforced.

authorizationParser Func<HttpContext, string, T>

The function delegate that will parse the authorization header of a web request and return the credentials of T.

principalParser TesterFunc<HttpContext, T, ConditionalValue<ClaimsPrincipal>, bool>

The function delegate that will parse the credentials of T returned from authorizationParser and if successful returns a ClaimsPrincipal object.

principal ConditionalValue<ClaimsPrincipal>

The ClaimsPrincipal of the authenticated user if the authentication was successful.

Returns

bool

true if the specified parameters triggers a successful authentication; otherwise, false.

Type Parameters

T

The type of the credentials returned from authorizationParser and passed to principalParser.