| | | 1 | | using System.Security.Claims; |
| | | 2 | | using ProjectTemplate.Infrastructure.Data; |
| | | 3 | | |
| | | 4 | | namespace ProjectTemplate.Web.Accessors; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// An implementation of <see cref="ICurrentActorAccessor"/> that retrieves the current actor information from the HTTP |
| | | 8 | | /// </summary> |
| | | 9 | | /// <param name="httpContextAccessor"></param> |
| | 28 | 10 | | public sealed class HttpContextCurrentActorAccessor( |
| | 28 | 11 | | IHttpContextAccessor httpContextAccessor) |
| | | 12 | | : ICurrentActorAccessor |
| | | 13 | | { |
| | | 14 | | private const string _subjectClaimType = "sub"; |
| | | 15 | | private const string _unknownActor = "Unknown"; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Accesses the current actor information from the HTTP context. It first attempts to retrieve the authenticated |
| | | 19 | | /// subject claim from the user's claims, then falls back to the authenticated name identifier claim, then the remot |
| | | 20 | | /// IP address. If none are available, it returns "Unknown". |
| | | 21 | | /// </summary> |
| | | 22 | | public string CurrentActor |
| | | 23 | | { |
| | | 24 | | get |
| | | 25 | | { |
| | 16 | 26 | | HttpContext? httpContext = httpContextAccessor.HttpContext; |
| | | 27 | | |
| | 16 | 28 | | string? authenticatedActor = GetAuthenticatedActor(httpContext?.User); |
| | | 29 | | |
| | 16 | 30 | | if (!string.IsNullOrWhiteSpace(authenticatedActor)) |
| | | 31 | | { |
| | 8 | 32 | | return authenticatedActor; |
| | | 33 | | } |
| | | 34 | | |
| | 8 | 35 | | string? remoteIpAddress = httpContext?.Connection.RemoteIpAddress?.ToString(); |
| | | 36 | | |
| | 8 | 37 | | return !string.IsNullOrWhiteSpace(remoteIpAddress) |
| | 8 | 38 | | ? $"Remote IP: {remoteIpAddress}" |
| | 8 | 39 | | : _unknownActor; |
| | | 40 | | } |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | private static string? GetAuthenticatedActor(ClaimsPrincipal? user) |
| | | 44 | | { |
| | 16 | 45 | | if (user?.Identity?.IsAuthenticated != true) |
| | | 46 | | { |
| | 6 | 47 | | return null; |
| | | 48 | | } |
| | | 49 | | |
| | 10 | 50 | | string? subject = GetClaimValue(user, _subjectClaimType); |
| | | 51 | | |
| | 10 | 52 | | if (!string.IsNullOrWhiteSpace(subject)) |
| | | 53 | | { |
| | 4 | 54 | | return $"Subject: {subject}"; |
| | | 55 | | } |
| | | 56 | | |
| | 6 | 57 | | string? nameIdentifier = GetClaimValue(user, ClaimTypes.NameIdentifier); |
| | | 58 | | |
| | 6 | 59 | | return !string.IsNullOrWhiteSpace(nameIdentifier) |
| | 6 | 60 | | ? $"Name Identifier: {nameIdentifier}" |
| | 6 | 61 | | : null; |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | private static string? GetClaimValue(ClaimsPrincipal user, string claimType) |
| | | 65 | | { |
| | 16 | 66 | | string? value = user.FindFirst(claimType)?.Value?.Trim(); |
| | | 67 | | |
| | 16 | 68 | | return string.IsNullOrWhiteSpace(value) ? null : value; |
| | | 69 | | } |
| | | 70 | | } |