-
Notifications
You must be signed in to change notification settings - Fork 25
Add UserClaims enricher to log authenticated user claim values #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,75 @@ | ||||||
| using System.Collections.Generic; | ||||||
| using System.Security.Claims; | ||||||
| using Microsoft.AspNetCore.Http; | ||||||
| using Serilog.Core; | ||||||
| using Serilog.Events; | ||||||
|
|
||||||
| namespace Serilog.Enrichers; | ||||||
|
|
||||||
| /// <inheritdoc /> | ||||||
| public class UserClaimsEnricher : ILogEventEnricher | ||||||
| { | ||||||
| private readonly Dictionary<string, string> _claimItemKeys; | ||||||
| private readonly string[] _claimNames; | ||||||
| private readonly IHttpContextAccessor _contextAccessor; | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Initializes a new instance of the <see cref="UserClaimsEnricher" /> class. | ||||||
| /// </summary> | ||||||
| /// <param name="claimNames">The names of the claims to log.</param> | ||||||
| public UserClaimsEnricher(params string[] claimNames) | ||||||
| : this(new HttpContextAccessor(), claimNames) | ||||||
| { | ||||||
| } | ||||||
|
|
||||||
| internal UserClaimsEnricher(IHttpContextAccessor contextAccessor, params string[] claimNames) | ||||||
| { | ||||||
| _contextAccessor = contextAccessor; | ||||||
| _claimNames = claimNames ?? []; | ||||||
| _claimItemKeys = new(); | ||||||
|
|
||||||
| // Pre-compute item keys for each claim | ||||||
| foreach (string claimName in _claimNames) | ||||||
| { | ||||||
| _claimItemKeys[claimName] = $"Serilog_UserClaim_{claimName}"; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// <inheritdoc /> | ||||||
| public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) | ||||||
| { | ||||||
| HttpContext httpContext = _contextAccessor.HttpContext; | ||||||
| if (httpContext == null) | ||||||
| { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| ClaimsPrincipal user = httpContext.User; | ||||||
| if (user == null || !user.Identity?.IsAuthenticated == true) | ||||||
|
||||||
| if (user == null || !user.Identity?.IsAuthenticated == true) | |
| if (user == null || user.Identity?.IsAuthenticated != true) |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -8,7 +8,7 @@ | |||||
| <DebugType>embedded</DebugType> | ||||||
| <EmbedAllSources>true</EmbedAllSources> | ||||||
| <Version>2.6.0</Version> | ||||||
|
||||||
| <Version>2.6.0</Version> | |
| <Version>2.7.0</Version> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code doesn't validate that individual claim names in the array are not null or empty. If a null claim name is passed (e.g.,
new UserClaimsEnricher("claim1", null, "claim2")), this could cause issues:"Serilog_UserClaim_"which could cause collisionsuser.FindFirst(null)which may throw aSystem.ArgumentNullExceptionConsider adding validation: