1 // Copyright (c) .NET Foundation. All rights reserved.
2 // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3 
4 using System.Linq;
5 using Microsoft.AspNetCore.Identity;
6 
7 namespace System.Security.Claims
8 {
9     /// <summary>
10     /// Claims related extensions for <see cref="ClaimsPrincipal"/>.
11     /// </summary>
12     public static class PrincipalExtensions
13     {
14         /// <summary>
15         /// Returns the value for the first claim of the specified type otherwise null the claim is not present.
16         /// </summary>
17         /// <param name="principal">The <see cref="ClaimsPrincipal"/> instance this method extends.</param>
18         /// <param name="claimType">The claim type whose first value should be returned.</param>
19         /// <returns>The value of the first instance of the specified claim type, or null if the claim is not present.</returns>
FindFirstValue(this ClaimsPrincipal principal, string claimType)20         public static string FindFirstValue(this ClaimsPrincipal principal, string claimType)
21         {
22             if (principal == null)
23             {
24                 throw new ArgumentNullException(nameof(principal));
25             }
26             var claim = principal.FindFirst(claimType);
27             return claim != null ? claim.Value : null;
28         }
29 
30     }
31 }