< Summary

Information
Class: ProjectTemplate.Web.Accessors.HttpContextApplicationAuditContextAccessor
Assembly: ProjectTemplate.Web
File(s): /home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/src/ProjectTemplate.Web/Accessors/HttpContextApplicationAuditContextAccessor.cs
Line coverage
5%
Covered lines: 2
Uncovered lines: 32
Coverable lines: 34
Total lines: 69
Line coverage: 5.8%
Branch coverage
0%
Covered branches: 0
Total branches: 42
Branch coverage: 0%
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_Current()0%930300%
GetAuthenticatedClaim(...)0%156120%

File(s)

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

#LineLine coverage
 1using System.Diagnostics;
 2using System.Security.Claims;
 3using ProjectTemplate.Infrastructure.Data.Auditing;
 4
 5namespace ProjectTemplate.Web.Accessors;
 6
 7/// <summary>
 8/// Resolves structured application audit context from the active HTTP request.
 9/// </summary>
 1210public sealed class HttpContextApplicationAuditContextAccessor(
 1211    IHttpContextAccessor httpContextAccessor)
 12    : IApplicationAuditContextAccessor
 13{
 14    private const string _subjectClaimType = "sub";
 15
 16    public ApplicationAuditContext Current
 17    {
 18        get
 19        {
 020            HttpContext? httpContext = httpContextAccessor.HttpContext;
 021            ClaimsPrincipal? user = httpContext?.User;
 22
 023            string? subject = GetAuthenticatedClaim(user, _subjectClaimType)
 024                ?? GetAuthenticatedClaim(user, ClaimTypes.NameIdentifier);
 25
 026            if (!string.IsNullOrWhiteSpace(subject))
 27            {
 028                return new ApplicationAuditContext(
 029                    subject,
 030                    ApplicationAuditActorTypes.Human,
 031                    subject,
 032                    correlationId: httpContext?.TraceIdentifier,
 033                    traceId: Activity.Current?.TraceId.ToString(),
 034                    spanId: Activity.Current?.SpanId.ToString());
 35            }
 36
 037            string? remoteIp = httpContext?.Connection.RemoteIpAddress?.ToString();
 38
 039            return !string.IsNullOrWhiteSpace(remoteIp)
 040                ? new ApplicationAuditContext(
 041                    remoteIp,
 042                    ApplicationAuditActorTypes.Network,
 043                    $"Remote IP: {remoteIp}",
 044                    correlationId: httpContext?.TraceIdentifier,
 045                    traceId: Activity.Current?.TraceId.ToString(),
 046                    spanId: Activity.Current?.SpanId.ToString())
 047                : new ApplicationAuditContext(
 048                "Unknown",
 049                ApplicationAuditActorTypes.Unknown,
 050                "Unknown",
 051                correlationId: httpContext?.TraceIdentifier,
 052                traceId: Activity.Current?.TraceId.ToString(),
 053                spanId: Activity.Current?.SpanId.ToString());
 54        }
 55    }
 56
 57    private static string? GetAuthenticatedClaim(
 58        ClaimsPrincipal? user,
 59        string claimType)
 60    {
 061        if (user?.Identity?.IsAuthenticated != true)
 62        {
 063            return null;
 64        }
 65
 066        string? value = user.FindFirst(claimType)?.Value?.Trim();
 067        return string.IsNullOrWhiteSpace(value) ? null : value;
 68    }
 69}