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
contextHttpContextThe context of the ASP.NET application.
requireSecureConnectionboolWhen
true, the HTTP connection is required to use secure sockets (that is, HTTPS); whenfalseno requirement is enforced.authorizationParserFunc<HttpContext, string, T>The function delegate that will parse the authorization header of a web request and return the credentials of
T.principalParserTesterFunc<HttpContext, T, ConditionalValue<ClaimsPrincipal>, bool>The function delegate that will parse the credentials of
Treturned fromauthorizationParserand if successful returns a ClaimsPrincipal object.
Returns
- ConditionalValue<ClaimsPrincipal>
A ClaimsPrincipal if
principalParserwas successful.
Type Parameters
TThe type of the credentials returned from
authorizationParserand passed toprincipalParser.
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
contextHttpContextThe context of the ASP.NET application.
requireSecureConnectionboolWhen
true, the HTTP connection is required to use secure sockets (that is, HTTPS); whenfalseno requirement is enforced.authorizationParserFunc<HttpContext, string, T>The function delegate that will parse the authorization header of a web request and return the credentials of
T.principalParserTesterFunc<HttpContext, T, ConditionalValue<ClaimsPrincipal>, bool>The function delegate that will parse the credentials of
Treturned fromauthorizationParserand if successful returns a ClaimsPrincipal object.principalConditionalValue<ClaimsPrincipal>The ClaimsPrincipal of the authenticated user if the authentication was successful.
Returns
- bool
trueif the specified parameters triggers a successful authentication; otherwise,false.
Type Parameters
TThe type of the credentials returned from
authorizationParserand passed toprincipalParser.