Skip to content

Add debugging attributes to System.Security.Claims types #86424

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

Merged
merged 5 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public ClaimsIdentity(System.Collections.Generic.IEnumerable<System.Security.Cla
public ClaimsIdentity(System.Collections.Generic.IEnumerable<System.Security.Claims.Claim>? claims, string? authenticationType) { }
public ClaimsIdentity(System.Collections.Generic.IEnumerable<System.Security.Claims.Claim>? claims, string? authenticationType, string? nameType, string? roleType) { }
public ClaimsIdentity(System.IO.BinaryReader reader) { }
[System.ObsoleteAttribute("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId = "SYSLIB0051", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
[System.ObsoleteAttribute("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
protected ClaimsIdentity(System.Runtime.Serialization.SerializationInfo info) { }
[System.ObsoleteAttribute("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId = "SYSLIB0051", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
[System.ObsoleteAttribute("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
protected ClaimsIdentity(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
protected ClaimsIdentity(System.Security.Claims.ClaimsIdentity other) { }
public ClaimsIdentity(System.Security.Principal.IIdentity? identity) { }
Expand Down Expand Up @@ -84,8 +84,8 @@ public partial class ClaimsPrincipal : System.Security.Principal.IPrincipal
public ClaimsPrincipal() { }
public ClaimsPrincipal(System.Collections.Generic.IEnumerable<System.Security.Claims.ClaimsIdentity> identities) { }
public ClaimsPrincipal(System.IO.BinaryReader reader) { }
[System.ObsoleteAttribute("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId = "SYSLIB0051", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
[System.ObsoleteAttribute("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
protected ClaimsPrincipal(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
public ClaimsPrincipal(System.Security.Principal.IIdentity identity) { }
public ClaimsPrincipal(System.Security.Principal.IPrincipal principal) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization;
using System.Security.Principal;
Expand All @@ -12,6 +13,8 @@ namespace System.Security.Claims
/// <summary>
/// An Identity that is represented by a set of claims.
/// </summary>
[DebuggerDisplay("{DebuggerToString(),nq}")]
[DebuggerTypeProxy(typeof(ClaimsIdentityDebugProxy))]
public class ClaimsIdentity : IIdentity
{
private enum SerializationMask
Expand Down Expand Up @@ -933,5 +936,38 @@ protected virtual void GetObjectData(SerializationInfo info, StreamingContext co
{
throw new PlatformNotSupportedException();
}

internal string DebuggerToString()
{
// DebuggerDisplayAttribute is inherited. Use virtual members instead of private fields to gather data.
int claimsCount = 0;
foreach (Claim item in Claims)
{
claimsCount++;
}

return $"Identity Name = {Name ?? "(null)"}, IsAuthenticated = {IsAuthenticated}, Claims Count = {claimsCount}";
}

private sealed class ClaimsIdentityDebugProxy
{
private readonly ClaimsIdentity _identity;

public ClaimsIdentityDebugProxy(ClaimsIdentity identity)
{
_identity = identity;
}

public ClaimsIdentity? Actor => _identity.Actor;
public string? AuthenticationType => _identity.AuthenticationType;
public object? BootstrapContext => _identity.BootstrapContext;
// List type has a friendly debugger view
public List<Claim> Claims => new List<Claim>(_identity.Claims);
public bool IsAuthenticated => _identity.IsAuthenticated;
public string? Label => _identity.Label;
public string? Name => _identity.Name;
public string NameClaimType => _identity.NameClaimType;
public string RoleClaimType => _identity.RoleClaimType;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization;
using System.Security.Principal;
Expand All @@ -13,6 +14,8 @@ namespace System.Security.Claims
/// <summary>
/// Concrete IPrincipal supporting multiple claims-based identities
/// </summary>
[DebuggerDisplay("{DebuggerToString(),nq}")]
[DebuggerTypeProxy(typeof(ClaimsPrincipalDebugProxy))]
public class ClaimsPrincipal : IPrincipal
{
private enum SerializationMask
Expand Down Expand Up @@ -567,5 +570,44 @@ protected virtual void GetObjectData(SerializationInfo info, StreamingContext co
{
throw new PlatformNotSupportedException();
}

private string DebuggerToString()
{
// DebuggerDisplayAttribute is inherited. Use virtual members instead of private fields to gather data.
int identitiesCount = 0;
foreach (ClaimsIdentity items in Identities)
{
identitiesCount++;
}

int claimsCount = 0;
foreach (Claim item in Claims)
{
claimsCount++;
}

// Return debug string optimized for the case of one identity.
if (identitiesCount == 1 && Identity is ClaimsIdentity claimsIdentity)
{
return $"Principal {claimsIdentity.DebuggerToString()}";
}

return $"Principal Identities Count: {identitiesCount}, Claims Count: {claimsCount}";
}

private sealed class ClaimsPrincipalDebugProxy
{
private readonly ClaimsPrincipal _principal;

public ClaimsPrincipalDebugProxy(ClaimsPrincipal principal)
{
_principal = principal;
}

// List type has a friendly debugger view
public List<Claim> Claims => new List<Claim>(_principal.Claims);
public List<ClaimsIdentity> Identities => new List<ClaimsIdentity>(_principal.Identities);
public IIdentity? Identity => _principal.Identity;
}
}
}