< Summary

Information
Class: ProjectTemplate.Web.Accessors.HttpContextCurrentActorAccessor
Assembly: ProjectTemplate.Web
File(s): /home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/src/ProjectTemplate.Web/Accessors/HttpContextCurrentActorAccessor.cs
Line coverage
100%
Covered lines: 22
Uncovered lines: 0
Coverable lines: 22
Total lines: 74
Line coverage: 100%
Branch coverage
92%
Covered branches: 26
Total branches: 28
Branch coverage: 92.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_CurrentActor()100%1010100%
GetAuthenticatedActor(...)91.66%1212100%
GetClaimValue(...)83.33%66100%

File(s)

/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/src/ProjectTemplate.Web/Accessors/HttpContextCurrentActorAccessor.cs

#LineLine coverage
 1using System.Security.Claims;
 2using ProjectTemplate.Infrastructure.Data;
 3using ProjectTemplate.Web.Authentication.Claims;
 4
 5namespace ProjectTemplate.Web.Accessors;
 6
 7/// <summary>
 8/// An implementation of <see cref="ICurrentActorAccessor"/> that retrieves the current actor information from the HTTP 
 9/// </summary>
 10/// <param name="httpContextAccessor"></param>
 2011public sealed class HttpContextCurrentActorAccessor(
 2012    IHttpContextAccessor httpContextAccessor)
 13    : ICurrentActorAccessor
 14{
 15    private const string _subjectClaimType = "sub";
 16    private const string _unknownActor = "Unknown";
 17
 18    /// <summary>
 19    /// Accesses the current actor information from the HTTP context. It first attempts to retrieve the authenticated
 20    /// normalized application subject claim, then the provider subject claim, then falls back to the authenticated name
 21    /// IP address. If none are available, it returns "Unknown".
 22    /// </summary>
 23    public string CurrentActor
 24    {
 25        get
 26        {
 1027            HttpContext? httpContext = httpContextAccessor.HttpContext;
 28
 1029            string? authenticatedActor = GetAuthenticatedActor(httpContext?.User);
 30
 1031            if (!string.IsNullOrWhiteSpace(authenticatedActor))
 32            {
 633                return authenticatedActor;
 34            }
 35
 436            string? remoteIpAddress = httpContext?.Connection.RemoteIpAddress?.ToString();
 37
 438            return !string.IsNullOrWhiteSpace(remoteIpAddress)
 439                ? $"Remote IP: {remoteIpAddress}"
 440                : _unknownActor;
 41        }
 42    }
 43
 44    private static string? GetAuthenticatedActor(ClaimsPrincipal? user)
 45    {
 1046        if (user?.Identity?.IsAuthenticated != true)
 47        {
 348            return null;
 49        }
 50
 51        // Prefer the normalized application claim: when claims transformation removes the original claims,
 52        // the provider "sub" and name identifier claims are no longer present.
 753        string? subject = GetClaimValue(user, ApplicationClaimTypes.Subject)
 754            ?? GetClaimValue(user, _subjectClaimType);
 55
 756        if (!string.IsNullOrWhiteSpace(subject))
 57        {
 458            return $"Subject: {subject}";
 59        }
 60
 361        string? nameIdentifier = GetClaimValue(user, ClaimTypes.NameIdentifier);
 62
 363        return !string.IsNullOrWhiteSpace(nameIdentifier)
 364            ? $"Name Identifier: {nameIdentifier}"
 365            : null;
 66    }
 67
 68    private static string? GetClaimValue(ClaimsPrincipal user, string claimType)
 69    {
 1570        string? value = user.FindFirst(claimType)?.Value?.Trim();
 71
 1572        return string.IsNullOrWhiteSpace(value) ? null : value;
 73    }
 74}