Aracılığıyla paylaş


Nasıl yapılır: Beyanları Karşılaştırma

Windows Communication Foundation'daki (WCF) Kimlik Modeli altyapısı, yetkilendirme denetimi gerçekleştirmek için kullanılır. Bu nedenle, yaygın bir görev yetkilendirme bağlamındaki talepleri istenen eylemi gerçekleştirmek veya istenen kaynağa erişmek için gereken taleplerle karşılaştırmaktır. Bu konuda, yerleşik ve özel talep türleri dahil olmak üzere taleplerin nasıl karşılaştırıldığı açıklanmaktadır. Kimlik Modeli altyapısı hakkında daha fazla bilgi için bkz . Kimlik Modeli ile Talepleri ve Yetkilendirmeyi Yönetme.

Talep karşılaştırması, bir talebin üç kısmının (tür, sağ ve kaynak) eşit olup olmadığını görmek için başka bir talepteki aynı parçalarla karşılaştırılmasını içerir. Aşağıdaki örneğe bakın.

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

Her iki talepte de talep türü Name, sağ ve PossessProperty"birisi" dizesinin kaynağı bulunur. Talebin üç bölümü de eşit olduğundan, taleplerin kendisi de eşittir.

Yerleşik talep türleri yöntemi kullanılarak Equals karşılaştırılır. Taleplere özgü karşılaştırma kodu gerektiğinde kullanılır. Örneğin, aşağıdaki iki kullanıcı asıl adı (UPN) talebi göz önünde bulundurulduğunda yöntemindeki Equals karşılaştırma kodu, ile aynı etki alanı kullanıcısını someone@example.comtanımlamış olduğu varsayılarak example\someone döndürürtrue.

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

Özel talep türleri yöntemi kullanılarak Equals da karşılaştırılabilir. Ancak, talebin özelliği tarafından Resource döndürülen türün ilkel bir tür dışında bir şey olduğu durumlarda, Equals yalnızca özellikler tarafından Resource döndürülen değerler yönteme Equals göre eşitse döndürürtrue. Bunun uygun olmadığı durumlarda, özelliği tarafından Resource döndürülen özel tür, gerekli olan özel işlemeyi gerçekleştirmek için ve GetHashCode yöntemlerini geçersiz kılmalıdırEquals.

Yerleşik talepleri karşılaştırma

  1. Sınıfının iki örneği Claim verüldüğünde, aşağıdaki kodda gösterildiği gibi karşılaştırma yapmak için öğesini kullanın Equals .

    public bool CompareTwoClaims(Claim c1, Claim c2)
    {
        return c1.Equals(c2);
    }
    
    Public Function CompareTwoClaims(ByVal c1 As Claim, ByVal c2 As Claim) As Boolean
        Return c1.Equals(c2)
    End Function
    

Özel talepleri ilkel kaynak türleriyle karşılaştırma

  1. Temel kaynak türlerine sahip özel talepler için, aşağıdaki kodda gösterildiği gibi yerleşik talepler için karşılaştırma gerçekleştirilebilir.

    public bool CompareTwoClaims(Claim c1, Claim c2)
    {
        return c1.Equals(c2);
    }
    
    Public Function CompareTwoClaims(ByVal c1 As Claim, _
    ByVal c2 As Claim) As Boolean
        Return c1.Equals(c2)
    
    End Function
    
  2. Yapı veya sınıf tabanlı kaynak türlerine sahip özel talepler için kaynak türü yöntemini geçersiz kılmalıdır Equals .

  3. İlk olarak parametresinin objnullolup olmadığını denetleyin ve varsa döndür.false

    if (obj == null) return false;
    
    If obj Is Nothing Then
        Return False
    
  4. Sonraki çağrı ReferenceEquals ve geçiş this ve obj parametre olarak. döndürürse true, döndürür true.

    if (ReferenceEquals(this, obj)) return true;
    
    If ReferenceEquals(Me, obj) Then
        Return True
    
  5. Bir sonraki deneme, sınıf türünün yerel değişkenine atama obj . Bu başarısız olursa, başvuru olur null. Bu gibi durumlarda döndür.false

  6. Geçerli talebi sağlanan taleple doğru şekilde karşılaştırmak için gereken özel karşılaştırmayı gerçekleştirin.

Örnek

Aşağıdaki örnekte, talep kaynağının ilkel olmayan bir tür olduğu özel taleplerin karşılaştırması gösterilmektedir.

using System;
using System.IdentityModel.Claims;

namespace Samples
{
    public sealed class MyResourceType
    {
        // private members
        private string text;
        private int number;

        // Constructors
        public MyResourceType()
        {
        }

        public MyResourceType(string text, int number)
        {
            this.text = text;
            this.number = number;
        }

        // Public properties
        public string Text { get { return this.text; } }
        public int Number { get { return this.number; } }

        // Override Object.Equals to perform specific comparison
        public override bool Equals(Object obj)
        {
            // If the object we're being asked to compare ourselves to is null
            // then return false
            if (obj == null)
                return false;

            // If the object we're being asked to compare ourselves to is us
            // then return true
            if (ReferenceEquals(this, obj))
                return true;

            // Try to convert the object we're being asked to compare ourselves to
            // into an instance of MyResourceType
            MyResourceType rhs = obj as MyResourceType;

            // If the object we're being asked to compare ourselves to
            // isn't an instance of MyResourceType then return false
            if (rhs == null)
                return false;

            // Return true if our members are the same as those of the object
            // we're being asked to compare ourselves to. Otherwise return false
            return (this.text == rhs.text && this.number == rhs.number);
        }

        public override int GetHashCode()
        {
            return (this.text.GetHashCode() ^ this.number.GetHashCode());
        }
    }

    class Program
    {
        public static void Main()
        {
            // Create two claims
            Claim c1 = new Claim("http://example.org/claims/mycustomclaim",
                new MyResourceType("Martin", 38), Rights.PossessProperty);
            Claim c2 = new Claim("http://example.org/claims/mycustomclaim",
                new MyResourceType("Martin", 38), Rights.PossessProperty);

            // Compare the claims
            if (c1.Equals(c2))
                Console.WriteLine("Claims are equal");
            else
                Console.WriteLine("Claims are not equal");
        }
    }
}
Imports System.IdentityModel.Claims
Imports System.Security.Permissions

NotInheritable Public Class MyResourceType
    ' private members
    Private textValue As String
    Private numberValue As Integer


    ' Constructors
    Public Sub New()

    End Sub

    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

Ayrıca bkz.