< 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: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 70
Line coverage: 100%
Branch coverage
92%
Covered branches: 24
Total branches: 26
Branch coverage: 92.3%
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(...)90%1010100%
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;
 3
 4namespace 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>
 2810public sealed class HttpContextCurrentActorAccessor(
 2811    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        {
 1626            HttpContext? httpContext = httpContextAccessor.HttpContext;
 27
 1628            string? authenticatedActor = GetAuthenticatedActor(httpContext?.User);
 29
 1630            if (!string.IsNullOrWhiteSpace(authenticatedActor))
 31            {
 832                return authenticatedActor;
 33            }
 34
 835            string? remoteIpAddress = httpContext?.Connection.RemoteIpAddress?.ToString();
 36
 837            return !string.IsNullOrWhiteSpace(remoteIpAddress)
 838                ? $"Remote IP: {remoteIpAddress}"
 839                : _unknownActor;
 40        }
 41    }
 42
 43    private static string? GetAuthenticatedActor(ClaimsPrincipal? user)
 44    {
 1645        if (user?.Identity?.IsAuthenticated != true)
 46        {
 647            return null;
 48        }
 49
 1050        string? subject = GetClaimValue(user, _subjectClaimType);
 51
 1052        if (!string.IsNullOrWhiteSpace(subject))
 53        {
 454            return $"Subject: {subject}";
 55        }
 56
 657        string? nameIdentifier = GetClaimValue(user, ClaimTypes.NameIdentifier);
 58
 659        return !string.IsNullOrWhiteSpace(nameIdentifier)
 660            ? $"Name Identifier: {nameIdentifier}"
 661            : null;
 62    }
 63
 64    private static string? GetClaimValue(ClaimsPrincipal user, string claimType)
 65    {
 1666        string? value = user.FindFirst(claimType)?.Value?.Trim();
 67
 1668        return string.IsNullOrWhiteSpace(value) ? null : value;
 69    }
 70}