Skip to content
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

Extended AspNetUserClaimLayoutRenderer to handle multi-values #975

Merged
merged 5 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 25 additions & 26 deletions src/Shared/LayoutRenderers/AspNetUserClaimLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ namespace NLog.Web.LayoutRenderers
/// ASP.NET User ClaimType Value Lookup.
/// </summary>
/// <remarks>
/// <code>${aspnet-user-claim:ClaimType=Name}</code>
/// <code>${aspnet-user-claim:ClaimType=Name}</code> to render a single specific claim type
/// <code>${aspnet-user-claim}</code> to render all claim types
/// </remarks>
/// <seealso href="https://github.com/NLog/NLog/wiki/AspNet-User-Claim-Layout-Renderer">Documentation on NLog Wiki</seealso>
[LayoutRenderer("aspnet-user-claim")]
Expand All @@ -28,19 +29,16 @@ public class AspNetUserClaimLayoutRenderer : AspNetLayoutMultiValueRendererBase
/// </summary>
/// <remarks>
/// When value is prefixed with "ClaimTypes." (Remember dot) then ít will lookup in well-known claim types from <see cref="ClaimTypes"/>. Ex. ClaimsTypes.Name
/// If this is null or empty then all claim types are rendered
/// </remarks>
[DefaultParameter]
public string ClaimType { get; set; }

/// <summary>
/// If this is set to true, then the ClaimType property is ignored and all Claim Types are rendered
/// </summary>
public bool All { get; set; }

/// <inheritdoc />
protected override void InitializeLayoutRenderer()
{
if (ClaimType?.Trim().StartsWith("ClaimTypes.", StringComparison.OrdinalIgnoreCase) == true || ClaimType?.Trim().StartsWith("ClaimType.", StringComparison.OrdinalIgnoreCase) == true)
if (ClaimType?.Trim().StartsWith("ClaimTypes.", StringComparison.OrdinalIgnoreCase) == true ||
ClaimType?.Trim().StartsWith("ClaimType.", StringComparison.OrdinalIgnoreCase) == true)
{
var fieldName = ClaimType.Substring(ClaimType.IndexOf('.') + 1).Trim();
var claimTypesField = typeof(ClaimTypes).GetField(fieldName, BindingFlags.Static | BindingFlags.Public);
Expand All @@ -65,26 +63,9 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
return;
}

if (All)
if (string.IsNullOrEmpty(ClaimType))
{
var claimKeyValuePairs =
new List<KeyValuePair<string, string>>();
#if NET46
// This is an IPrincipal in NET 46, need to cast
if (claimsPrincipal is ClaimsPrincipal)
{
foreach (var claim in (claimsPrincipal as ClaimsPrincipal).Claims)
{
claimKeyValuePairs.Add(new KeyValuePair<string, string>(claim.Type, claim.Value));
}
}
#else
foreach (var claim in claimsPrincipal.Claims)
{
claimKeyValuePairs.Add(new KeyValuePair<string, string>(claim.Type, claim.Value));
}
#endif
SerializePairs(claimKeyValuePairs, builder, logEvent);
SerializePairs(GetAllClaims(claimsPrincipal), builder, logEvent);
}
else
{
Expand All @@ -100,6 +81,24 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
InternalLogger.Debug(ex, "aspnet-user-claim - HttpContext has been disposed");
}
}

#if NET46
private IEnumerable<KeyValuePair<string, string>> GetAllClaims(IPrincipal claimsPrincipal)
{
if (claimsPrincipal is ClaimsPrincipal)
snakefoot marked this conversation as resolved.
Show resolved Hide resolved
{
return (claimsPrincipal as ClaimsPrincipal)?.Claims?.Select(claim => new KeyValuePair<string, string>(claim.Type, claim.Value));
}

return new List<KeyValuePair<string, string>>();
}
#else
private IEnumerable<KeyValuePair<string, string>> GetAllClaims(ClaimsPrincipal claimsPrincipal)
{
return claimsPrincipal?.Claims?.Select(claim => new KeyValuePair<string, string>(claim.Type, claim.Value));
}
#endif

#if NET46
private Claim GetClaim(IPrincipal claimsPrincipal, string claimType)
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public void AllRendersAllValue()
{
// Arrange
var (renderer, httpContext) = CreateWithHttpContext();
renderer.All = true;

var expectedResult = "http://schemas.xmlsoap.org/ws/2009/09/identity/claims/actor=ActorValue1,http://schemas.xmlsoap.org/ws/2009/09/identity/claims/actor=ActorValue2,http://schemas.xmlsoap.org/ws/2005/05/identity/claims/country=CountryValue";

Expand Down