AuthenticationHandlerFeature Class
Definition
- Namespace
- Cuemon.AspNetCore.Authentication
- Assemblies
- Cuemon.AspNetCore.Authentication.dll
Provides a combined default implementation of IAuthenticateResultFeature and IHttpAuthenticationFeature so that AuthenticateResult and User is consistent with each other.
public class AuthenticationHandlerFeature : IAuthenticateResultFeature, IHttpAuthenticationFeature
- Inheritance
-
AuthenticationHandlerFeature
- Implements
Examples
The following example demonstrates how to keep the authenticate result and user principal synchronized on the current HTTP features.
using System;
using System.Security.Claims;
using Cuemon.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features.Authentication;
namespace MyApp.Examples;
public static class AuthenticationHandlerFeatureExample
{
public static void Demonstrate()
{
var principal = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, "Agent") }, "Basic"));
var result = AuthenticateResult.Success(new AuthenticationTicket(principal, "Basic"));
var context = new DefaultHttpContext();
AuthenticationHandlerFeature.Set(result, context);
var authenticateFeature = (AuthenticationHandlerFeature)context.Features.Get<IAuthenticateResultFeature>()!;
var httpAuthenticationFeature = (AuthenticationHandlerFeature)context.Features.Get<IHttpAuthenticationFeature>()!;
Console.WriteLine(authenticateFeature.AuthenticateResult == result);
Console.WriteLine(httpAuthenticationFeature.User.Identity?.Name);
}
}
Remarks
Inspiration/cloned from https://github.com/dotnet/aspnetcore/blob/main/src/Security/Authentication/Core/src/AuthenticationFeatures.cs
Constructors
| Name | Description |
|---|---|
| AuthenticationHandlerFeature(AuthenticateResult) | Initializes a new instance of the AuthenticationHandlerFeature class. |
Properties
| Name | Description |
|---|---|
| AuthenticateResult | The AuthenticateResult from the authorization middleware. |
| User | Gets or sets the ClaimsPrincipal associated with the HTTP request. |
Methods
| Name | Description |
|---|---|
| Set(AuthenticateResult, HttpContext) | Provides a convenient and consistent way of propagating HTTP features for IAuthenticateResultFeature and IHttpAuthenticationFeature. |