Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

How to: Compare Claims

The Identity Model infrastructure in Windows Communication Foundation (WCF) is used to perform authorization checking. As such, a common task is to compare claims in the authorization context to the claims required to perform the requested action or access the requested resource. This topic describes how to compare claims, including built-in and custom claim types. For more information about the Identity Model infrastructure, see Managing Claims and Authorization with the Identity Model.

Claim comparison involves comparing the three parts of a claim (type, right, and resource) against the same parts in another claim to see if they are equal. See the following example.


Dim c1 As Claim = Claim.CreateNameClaim("someone")
Dim c2 As Claim = Claim.CreateNameClaim("someone")


Both claims have a claim type of Name, a right of PossessProperty, and a resource of the string "someone". As all three parts of the claim are equal, the claims themselves are equal.

The built-in claim types are compared using the Equals method. Claim-specific comparison code is used where necessary. For example, given the following two user principal name (UPN) claims:


Dim c1 As Claim = Claim.CreateUpnClaim("someone@example.com")
Dim c2 As Claim = Claim.CreateUpnClaim("example\someone")


the comparison code in the Equals method returns true, assuming example\someone identifies the same domain user as "someone@example.com".

Custom claim types can also be compared using the Equals method. However, in cases where the type returned by the Resource property of the claim is something other than a primitive type, the Equals returns true only if the values returned by the Resource properties are equal according to the Equals method. In cases where this is not appropriate, the custom type returned by the Resource property should override the Equals and GetHashCode methods to perform whatever custom processing is necessary.

Comparing built-in claims

  • Given two instances of the Claim class, use the Equals to make the comparison, as shown in the following code.

    
    Public Function CompareTwoClaims(ByVal c1 As Claim, ByVal c2 As Claim) As Boolean 
        Return c1.Equals(c2)        
    End Function 
    
    
    

Comparing custom claims with primitive resource types

  1. For custom claims with primitive resource types, comparison can be performed as for built-in claims, as shown in the following code.

    
    Public Function CompareTwoClaims(ByVal c1 As Claim, _
    ByVal c2 As Claim) As Boolean 
        Return c1.Equals(c2)
    
    End Function         
    
    
    
  2. For custom claims with structure or class based resource types, the resource type should override the Equals method.

  3. First check whether the obj parameter is null, and if so, return false.

    
    If obj Is Nothing Then
        Return False
    
    
    
  4. Next call ReferenceEquals and pass this and obj as parameters. If it returns true, then return true.

    
    If ReferenceEquals(Me, obj) Then
        Return True
    
    
    
  5. Next attempt to assign obj to a local variable of the class type. If this fails, the reference is null. In such cases, return false.

  6. Perform the custom comparison necessary to correctly compare the current claim to the provided claim.

The following example shows a comparison of custom claims where the claim resource is a non-primitive type.


Imports System
Imports System.IdentityModel.Claims
Imports System.Security.Permissions

<assembly: SecurityPermission(SecurityAction.RequestMinimum, Execution := True)>
NotInheritable Public Class MyResourceType
    ' private members
    Private textValue As String
    Private numberValue As Integer


    ' Constructors
    Public Sub New() 

    End Sub 'New

    Public Sub New(ByVal textVal As String, ByVal numberValue As Integer) 
        Me.textValue = textVal
        Me.numberValue = numberValue

    End Sub 

    ' Public properties

    Public ReadOnly Property Text() As String 
        Get
            Return Me.textValue
        End Get
    End Property

    Public ReadOnly Property Number() As Integer 
        Get
            Return Me.numberValue
        End Get 
    End Property
    ' Override Object.Equals to perform a specific comparison.
    Public Overrides Function Equals(ByVal obj As [Object]) As Boolean 
        ' If the object being compared to is null then return false.
        If obj Is Nothing Then
            Return False
        End If 
        ' If the object we are being asked to compare ourselves to is us
        ' then return true.
        If ReferenceEquals(Me, obj) Then
            Return True
        End If 
        ' Try to convert the object we are being asked to compare ourselves to
        ' into an instance of MyResourceType.
        Dim rhs As MyResourceType = CType(obj, MyResourceType)

        ' If the object being compared to is not an instance of 
        ' MyResourceType then return false.
        If rhs Is Nothing Then
            Return False
        End If 
        ' Return true if members are the same as those of the object
        ' being asked to compare to; otherwise, return false.
        Return Me.textValue = rhs.textValue AndAlso Me.numberValue = rhs.numberValue

    End Function 

    Public Overrides Function GetHashCode() As Integer 
        Return Me.textValue.GetHashCode() ^ Me.numberValue.GetHashCode()

    End Function 
End Class 
Class Program

    Public Shared Sub Main() 
        ' Create two claims.
        Dim c1 As New Claim("http://example.org/claims/mycustomclaim", _
           New MyResourceType("Martin", 38), Rights.PossessProperty)
        Dim c2 As New Claim("http://example.org/claims/mycustomclaim", _
           New MyResourceType("Martin", 38), Rights.PossessProperty)

        ' Compare the claims.
        If c1.Equals(c2) Then
            Console.WriteLine("Claims are equal")
        Else
            Console.WriteLine("Claims are not equal")
        End If

    End Sub 
End Class 


Show:
© 2015 Microsoft