Current User
This commit is contained in:
@@ -0,0 +1,43 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
using System.Security.Claims;
|
||||||
|
|
||||||
|
namespace WebApi.Authentication;
|
||||||
|
|
||||||
|
public sealed class CurrentUser : IBindableFromHttpContext<CurrentUser>
|
||||||
|
{
|
||||||
|
private CurrentUser(ClaimsPrincipal principal)
|
||||||
|
{
|
||||||
|
Principal = principal;
|
||||||
|
var isAuthenticated = principal.Identity?.IsAuthenticated;
|
||||||
|
IsAuthenticated = isAuthenticated.HasValue && isAuthenticated.Value;
|
||||||
|
|
||||||
|
var rawId = principal.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||||
|
Id = Guid.TryParse(rawId, out var id)? id : Guid.Empty;
|
||||||
|
|
||||||
|
Username = principal.FindFirstValue(ClaimTypes.Name);
|
||||||
|
FirstName = principal.FindFirstValue(ClaimTypes.GivenName);
|
||||||
|
LastName = principal.FindFirstValue(ClaimTypes.Surname);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ClaimsPrincipal Principal { get; }
|
||||||
|
|
||||||
|
public bool IsAuthenticated { get; }
|
||||||
|
|
||||||
|
public Guid? Id { get; }
|
||||||
|
|
||||||
|
public string? Username { get; }
|
||||||
|
|
||||||
|
public string? FirstName { get; }
|
||||||
|
|
||||||
|
public string? LastName { get; }
|
||||||
|
|
||||||
|
public Guid RequiredId => Id
|
||||||
|
?? throw new InvalidOperationException("The current request does not contain an authenticated user id.");
|
||||||
|
|
||||||
|
public bool HasClaim(string type) => Principal.HasClaim(c => c.Type == type);
|
||||||
|
|
||||||
|
public string? FindClaim(string type) => Principal.FindFirstValue(type);
|
||||||
|
|
||||||
|
public static ValueTask<CurrentUser?> BindAsync(HttpContext context, ParameterInfo parameter) =>
|
||||||
|
ValueTask.FromResult<CurrentUser?>(new CurrentUser(context.User));
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user