src/NuGetGallery.Services/Extensions/PrincipalExtensions.cs
/// <summary>
/// Try to add a new default claim to the identity. It will not replace an existing claim.
/// </summary>
/// <param name="identity">IIdentity from which the claim is to be removed</param>
/// <param name="claimType">The claim type to be added</param>
/// <param name="claimValue">The claim value for the type to be added, null will add default boolean claim</param>
/// <returns>True if successfully able to add the claim, false otherwise</returns>
Try to add a new default claim to the identity. It will not replace an existing claim.
Try to add a new default claim to the identity . It will not replace an existing claim .
public static bool TryAddClaim(this IIdentity identity, string claimType, string claimValue = null)
        {
            var claimsIdentity = identity as ClaimsIdentity;
            var existingClaim = claimsIdentity?.FindFirst(claimType);
            if (existingClaim == null)
            {
                var claim = string.IsNullOrEmpty(claimValue)
                    ? ClaimsExtensions.CreateBooleanClaim(claimType)
                    : new Claim(claimType, claimValue);
                claimsIdentity.AddClaim(claim);
                return true;
            }
            return false;
        }
public static bool TryAddClaim ( this IIdentity identity , string claimType , string claimValue = null ) { var claimsIdentity = identity as ClaimsIdentity ; var existingClaim = claimsIdentity ? . FindFirst ( claimType ) ; if ( existingClaim == null ) { var claim = string . IsNullOrEmpty ( claimValue ) ? ClaimsExtensions . CreateBooleanClaim ( claimType ) : new Claim ( claimType , claimValue ) ; claimsIdentity . AddClaim ( claim ) ; return true ; } return false ; }
Try to add a new default claim to the identity.
Try to add a new default claim to the identity .
True if successfully able to add the claim, false otherwise
True if successfully able to add the claim false otherwise
IIdentity
this
claimType
string
The claim type to be added
The claim type to be added
claimValue
string
The claim value for the type to be added, null will add default boolean claim
The claim value for the type to be added null will add default boolean claim
identity
IIdentity from which the claim is to be removed
IIdentity from which the claim is to be removed