Finding Claims in a ClaimSet

Examining the content of a ClaimSet for particular types of claims is a common task when using claim-based authorization. To examine a ClaimSet for the presence of particular claims, use the FindClaims method. This method provides better performance than iterating directly over the ClaimSet. The following example demonstrates this usage. Note that the claimType and claimRight parameters can be null. In that case, the parameters will match all claim types and claim rights.

Example

// The FindSomeClaims method looks in the provided ClaimSet for Claims of the specified type and right. 
   // It returns such Claims in a list.
public static IList<Claim> FindSomeClaims ( ClaimSet cs, string type, string right )
{
  // Create an empty list
  IList<Claim> claims = new List<Claim>();

  // Call ClaimSet.FindClaims with the specified type and right. Iterate over the result...
  foreach(Claim c in cs.FindClaims ( type, right ))
	//...adding each claim to the list
	claims.Add ( c );

  // Return the list
  return claims;
}
' The FindSomeClaims method looks in the provided ClaimSet for Claims of the specified type and right. 
' It returns such Claims in a list.
Public Shared Function FindSomeClaims(ByVal cs As ClaimSet, _
                                      ByVal type As String, _
                                      ByVal right As String) As IList(Of Claim)
    ' Create an empty list
    Dim claims As New List(Of Claim)()

    ' Call ClaimSet.FindClaims with the specified type and right. Iterate over the result...
    For Each c In cs.FindClaims(type, right)
        '...adding each claim to the list
        claims.Add(c)
    Next c

    ' Return the list
    Return claims
End Function

See Also

Managing Claims and Authorization with the Identity Model