-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid allocation in TryGetAuthenticatedUser.
As we reported in dotnet/runtime#107861 it turns out that retrieving ClaimsPrincipal.Identity causes an unnecessary allocation. This works around that.
- Loading branch information
Showing
2 changed files
with
46 additions
and
1 deletion.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
Solutions/Corvus.YarpPipelines/Corvus.YarpPipelines/ClaimsExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// <copyright file="ClaimsExtensions.cs" company="Endjin Limited"> | ||
// Copyright (c) Endjin Limited. All rights reserved. | ||
// </copyright> | ||
|
||
using System.Security.Claims; | ||
using System.Security.Principal; | ||
|
||
namespace Corvus.YarpPipelines; | ||
|
||
/// <summary> | ||
/// Low-allocation utilities for working with claims. | ||
/// </summary> | ||
internal static class ClaimsExtensions | ||
{ | ||
/// <summary> | ||
/// A non-allocating version of <see cref="ClaimsPrincipal.Identity"/>. | ||
/// </summary> | ||
/// <param name="claimsPrincipal"> | ||
/// The principal from which to extract the primary identity. | ||
/// </param> | ||
/// <returns>The primary identity.</returns> | ||
/// <remarks> | ||
/// <see cref="ClaimsPrincipal.Identity"/> allocates an enumerator, because | ||
/// it executes a foreach over the identities. In most cases, these identities | ||
/// are in an IList, making zero-allocation enumeration possible, and since | ||
/// the .NET runtime implementation doesn't do that (at least, not in .NET 8.0) | ||
/// we have to do it ourselves. (See https://github.com/dotnet/runtime/issues/107861) | ||
/// </remarks> | ||
public static IIdentity? GetPrimaryIdentity(this ClaimsPrincipal claimsPrincipal) | ||
{ | ||
if (claimsPrincipal.Identities is IList<ClaimsIdentity> identities) | ||
{ | ||
for (int i = 0; i < identities.Count; i++) | ||
{ | ||
if (identities[i] is not null) | ||
{ | ||
return identities[i]; | ||
} | ||
} | ||
} | ||
|
||
return claimsPrincipal.Identity; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters