/
githubmirror
/
aspnetcore
Обзор
Документация
Войти
/
githubmirror
/
aspnetcore
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Shared/SecurityHelper/SecurityHelper.cs
46 строк
2 KB
Pranav K
Use file scoped namespaces (#38076)
06 ноя 2021, 03:52
Не верифицирован
06 ноя 2021, 03:52
a450cb6
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. #nullable enable using System.Linq; using System.Security.Claims; namespace Microsoft.Extensions.Internal; /// <summary> /// Helper code used when implementing authentication middleware /// </summary> internal static class SecurityHelper { /// <summary> /// Add all ClaimsIdentities from an additional ClaimPrincipal to the ClaimsPrincipal /// Merges a new claims principal, placing all new identities first, and eliminating /// any empty unauthenticated identities from context.User /// </summary> /// <param name="existingPrincipal">The <see cref="ClaimsPrincipal"/> containing existing <see cref="ClaimsIdentity"/>.</param> /// <param name="additionalPrincipal">The <see cref="ClaimsPrincipal"/> containing <see cref="ClaimsIdentity"/> to be added.</param> public static ClaimsPrincipal MergeUserPrincipal(ClaimsPrincipal? existingPrincipal, ClaimsPrincipal? additionalPrincipal) { // For the first principal, just use the new principal rather than copying it if (existingPrincipal == null && additionalPrincipal != null) { return additionalPrincipal; } var newPrincipal = new ClaimsPrincipal(); // New principal identities go first if (additionalPrincipal != null) { newPrincipal.AddIdentities(additionalPrincipal.Identities); } // Then add any existing non empty or authenticated identities if (existingPrincipal != null) { newPrincipal.AddIdentities(existingPrincipal.Identities.Where(i => i.IsAuthenticated || i.Claims.Any())); } return newPrincipal; } }