System.Security.Claims.ClaimsPrincipalExtensions.FindFirstValue C# (CSharp) Method

FindFirstValue() public static method

public static FindFirstValue ( this principal, string claimType, bool throwIfNotFound = false ) : string
principal this
claimType string
throwIfNotFound bool
return string
        public static string FindFirstValue(this ClaimsPrincipal principal, string claimType, bool throwIfNotFound = false)
        {
            Guard.ArgumentNotNull(principal, nameof(principal));

            var value = principal.FindFirst(claimType)?.Value;
            if (throwIfNotFound && string.IsNullOrWhiteSpace(value))
            {
                throw new InvalidOperationException(
                    string.Format(CultureInfo.InvariantCulture, "The supplied principal does not contain a claim of type {0}", claimType));
            }

            return value;
        }