Skip to content

Commit

Permalink
Claims identity and principal debugging update (dotnet#87742)
Browse files Browse the repository at this point in the history
* Claims identity and principal debugging update

* Comment
  • Loading branch information
JamesNK authored Jun 19, 2023
1 parent eb71494 commit 3a76e98
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,21 @@ internal string DebuggerToString()
claimsCount++;
}

return $"Identity Name = {Name ?? "(null)"}, IsAuthenticated = {(IsAuthenticated ? "true" : "false")}, Claims Count = {claimsCount}";
string debugText = $"IsAuthenticated = {(IsAuthenticated ? "true" : "false")}";
if (Name != null)
{
// The ClaimsIdentity.Name property requires that ClaimsIdentity.NameClaimType is correctly
// configured to match the name of the logical name claim type of the identity.
// Because of this, only include name if the ClaimsIdentity.Name property has a value.
// Not including the name is to avoid developer confusion at seeing "Name = (null)" on an authenticated identity.
debugText += $", Name = {Name}";
}
if (claimsCount > 0)
{
debugText += $", Claims = {claimsCount}";
}

return debugText;
}

private sealed class ClaimsIdentityDebugProxy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,19 +580,19 @@ private string DebuggerToString()
identitiesCount++;
}

int claimsCount = 0;
foreach (Claim item in Claims)
// Return debug string optimized for the case of one identity.
if (identitiesCount == 1 && Identity is ClaimsIdentity claimsIdentity)
{
claimsCount++;
return claimsIdentity.DebuggerToString();
}

// Return debug string optimized for the case of one identity.
if (identitiesCount == 1 && Identity is ClaimsIdentity claimsIdentity)
int claimsCount = 0;
foreach (Claim item in Claims)
{
return $"Principal {claimsIdentity.DebuggerToString()}";
claimsCount++;
}

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

private sealed class ClaimsPrincipalDebugProxy
Expand Down

0 comments on commit 3a76e98

Please sign in to comment.