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;
}