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

TryRemoveClaim() public method

It is possible that a Claim returned from Claims cannot be removed. This would be the case for 'External' claims that are provided by reference.

object.ReferenceEquals is used to 'match'.

public TryRemoveClaim ( Claim claim ) : bool
claim Claim the to match.
return bool
        public virtual bool TryRemoveClaim(Claim claim)
        {
            if (claim == null)
            {
                return false;
            }

            bool removed = false;

            for (int i = 0; i < _instanceClaims.Count; i++)
            {
                if (object.ReferenceEquals(_instanceClaims[i], claim))
                {
                    _instanceClaims.RemoveAt(i);
                    removed = true;
                    break;
                }
            }
            return removed;
        }

Same methods

ClaimsIdentity::TryRemoveClaim ( System claim ) : bool

Usage Example

コード例 #1
0
 public static void ReplaceClaim(this ClaimsIdentity identity, string type, string value)
 {
     identity.TryRemoveClaim(identity.Claims.FirstOrDefault(c => c.Type == type));
     identity.AddClaim(new Claim(type, value));
 }