System.Security.Claims.ClaimsIdentity.HasClaim C# (CSharp) Method

HasClaim() public method

Determines if a claim with type AND value is contained within this ClaimsIdentity.
Comparison is: StringComparison.OrdinalIgnoreCase for Claim.Type, StringComparison.Ordinal for Claim.Value.
if 'type' is null. if 'value' is null.
public HasClaim ( string type, string value ) : bool
type string the type of the claim to match.
value string the value of the claim to match.
return bool
        public virtual bool HasClaim(string type, string value)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            Contract.EndContractBlock();

            foreach (Claim claim in Claims)
            {
                if (claim != null)
                {
                    if (claim != null
                         && string.Equals(claim.Type, type, StringComparison.OrdinalIgnoreCase)
                         && string.Equals(claim.Value, value, StringComparison.Ordinal))
                    {
                        return true;
                    }
                }
            }

            return false;
        }

Same methods

ClaimsIdentity::HasClaim ( Predicate match ) : bool
ClaimsIdentity::HasClaim ( System match ) : bool

Usage Example

Ejemplo n.º 1
0
 public static IEnumerable<Claim> CreateRolesForClaims(ClaimsIdentity user) {
     List<Claim> claims = new List<Claim>();
     if (user.HasClaim(x => x.Type == ClaimTypes.StateOrProvince && x.Issuer == "RemoteClaims" && x.Value == "DC")
         && user.HasClaim(x => x.Type == ClaimTypes.Role && x.Value == "Employees")) {
         claims.Add(new Claim(ClaimTypes.Role, "DCStaff"));
     }
     return claims;            
 }
All Usage Examples Of System.Security.Claims.ClaimsIdentity::HasClaim