ClaimTypes Sınıf

Tanım

Bir varlığın talepebileceği önceden tanımlanmış talep türlerini temsil eder. Bu sınıf devralınamaz.

public ref class ClaimTypes abstract sealed
public static class ClaimTypes
type ClaimTypes = class
Public Class ClaimTypes
Devralma
ClaimTypes

Örnekler


using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.IdentityModel.Claims;
using System.IdentityModel.Policy;
using System.IdentityModel.Tokens;
using System.IdentityModel.Selectors;
using System.ServiceModel;

namespace Microsoft.ServiceModel.Samples.SupportingTokens
{
    [ServiceContract]
    public interface IEchoService : IDisposable
    {
        [OperationContract]
        string Echo();
    }
    // Service class that implements the service contract.
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class EchoService : IEchoService
    {
        public string Echo()
        {
            string userName;
            string certificateSubjectName;
            GetCallerIdentities(OperationContext.Current.ServiceSecurityContext, out userName, out certificateSubjectName);
            return String.Format("Hello {0}, {1}", userName, certificateSubjectName);
        }

        public void Dispose()
        {
        }

        bool TryGetClaimValue<TClaimResource>(ClaimSet claimSet, string claimType, out TClaimResource resourceValue)
            where TClaimResource : class
        {
            resourceValue = default(TClaimResource);
            IEnumerable<Claim> matchingClaims = claimSet.FindClaims(claimType, Rights.PossessProperty);
            if (matchingClaims == null)
                return false;
            IEnumerator<Claim> enumerator = matchingClaims.GetEnumerator();
            if (enumerator.MoveNext())
            {
                resourceValue = (enumerator.Current.Resource == null) ? null : (enumerator.Current.Resource as TClaimResource);
                return true;
            }
            else
            {
                return false;
            }
        }

        // Returns the username and certificate subject name provided by the client.
        void GetCallerIdentities(ServiceSecurityContext callerSecurityContext, out string userName, out string certificateSubjectName)
        {
            userName = null;
            certificateSubjectName = null;

            // Look in all the claimsets in the authorization context.
            foreach (ClaimSet claimSet in callerSecurityContext.AuthorizationContext.ClaimSets)
            {
                // Try to find a Upn claim. This has been generated from the windows username.
                string tmpName;
                if (TryGetClaimValue<string>(claimSet, ClaimTypes.Upn, out tmpName))
                {
                    userName = tmpName;
                }
                else
                {
                    // Try to find an X500DistinguishedName claim. This has been generated from the client certificate.
                    X500DistinguishedName tmpDistinguishedName;
                    if (TryGetClaimValue<X500DistinguishedName>(claimSet, ClaimTypes.X500DistinguishedName, out tmpDistinguishedName))
                    {
                        certificateSubjectName = tmpDistinguishedName.Name;
                    }
                }
            }
        }
    }
}
Imports System.Collections.Generic
Imports System.Security.Cryptography.X509Certificates
Imports System.IdentityModel.Claims
Imports System.IdentityModel.Policy
Imports System.IdentityModel.Tokens
Imports System.IdentityModel.Selectors
Imports System.ServiceModel


' Service class that implements the service contract.
<ServiceBehavior(IncludeExceptionDetailInFaults:=True)> _
Public Class EchoService
    Implements IEchoService
    <ServiceContract()> _
    Public Interface IEchoService
        : Inherits IDisposable
        <OperationContract()> _
        Function Echo() As String
    End Interface 'IEchoService

    Public Function Echo() As String Implements IEchoService.Echo
        Dim userName As String = String.Empty
        Dim certificateSubjectName As String = String.Empty
        GetCallerIdentities(OperationContext.Current.ServiceSecurityContext, userName, certificateSubjectName)
        Return String.Format("Hello {0}, {1}", userName, certificateSubjectName)

    End Function 'Echo


    Public Sub Dispose() Implements IDisposable.Dispose

    End Sub


    

    Function TryGetClaimValue(Of TClaimResource)(ByVal claimSet As ClaimSet, ByVal claimType As String, ByRef resourceValue As TClaimResource) As Boolean
        Dim matchingClaims As IEnumerable(Of Claim) = claimSet.FindClaims(claimType, Rights.PossessProperty)
        If matchingClaims Is Nothing Then
            Return False
        End If
        Dim enumerator As IEnumerator(Of Claim) = matchingClaims.GetEnumerator()
        If enumerator.MoveNext() Then
            If enumerator.Current.Resource Is Nothing Then
                resourceValue = Nothing
            Else
                resourceValue = CType(enumerator.Current.Resource, TClaimResource)
            End If
            Return True
        Else
            Return False
        End If
    End Function
    Sub GetCallerIdentities(ByVal callerSecurityContext As ServiceSecurityContext, ByRef userName As String, ByRef certificateSubjectName As String)
        ' Returns the username and certificate subject name provided by the client.

        userName = Nothing
        certificateSubjectName = Nothing

        ' Look in all the claimsets in the authorization context.
        Dim claimSet As ClaimSet
        For Each claimSet In callerSecurityContext.AuthorizationContext.ClaimSets
            ' Try to find a Upn claim. This has been generated from the Windows username.
            Dim tmpName As String = String.Empty
            If TryGetClaimValue(Of String)(claimSet, ClaimTypes.Upn, tmpName) Then
                userName = tmpName
            Else
                ' Try to find an X500DistinguishedName claim. This has been generated from the client certificate.
                Dim tmpDistinguishedName As X500DistinguishedName = Nothing
                If TryGetClaimValue(Of X500DistinguishedName)(claimSet, ClaimTypes.X500DistinguishedName, tmpDistinguishedName) Then
                    certificateSubjectName = tmpDistinguishedName.Name
                End If
            End If
        Next claimSet

    End Sub
End Class

Açıklamalar

veya içinde ClaimTypes belirli bir talep ClaimSet türünü aramak için veya talep oluşturmak için sınıfını kullanın. içinde belirli bir talep ClaimSettürünü aramak için yöntemini kullanın FindClaims(String, String) ve parametresi için talep türünü belirtmek üzere bu sınıfın claimType özelliklerini kullanın. Sınıfın Claim oluşturucu yeni bir talep oluşturmak için kullanıldığında, parametresini belirtmek claimType için sınıfının özelliklerini ClaimTypes kullanın. Talep türlerinin çoğu için, sınıfı belirli bir türe Claim ait bir talep döndüren statik özelliklere sahiptir. Örneğin yöntemi, CreateHashClaim(Byte[]) talep türünü kullanarak Hash bir talep döndürür.

Özellikler

Anonymous

Anonim kullanıcıyı belirten bir talebin URI'sini alır.

Authentication

Kimliğin kimlik doğrulaması yapılıp yapılmadığıyla ilgili ayrıntıları belirten bir talebin URI'sini alır.

AuthorizationDecision

Varlıkta yetkilendirme kararını belirten bir talebin URI'sini alır.

Country

Bir varlığın bulunduğu ülkeyi/bölgeyi belirten talebin URI'sini alır.

DateOfBirth

Bir varlığın doğum tarihini belirten bir talebin URI'sini alır.

DenyOnlySid

Bir varlık için yalnızca reddetme güvenlik tanımlayıcısı (SID) belirten bir talebin URI'sini alır.

Dns

Bilgisayar adıyla ilişkili DNS adını veya X.509 sertifikasının konusunun veya verenin alternatif adını belirten bir talebin URI'sini alır.

Email

Bir varlığın e-posta adresini belirten talebin URI'sini alır.

Gender

Bir varlığın cinsiyetini belirten talebin URI'sini alır.

GivenName

Bir varlığın verilen adını belirten bir talebin URI'sini alır.

Hash

Karma değeri belirten bir talebin URI'sini alır.

HomePhone

Bir varlığın ev telefon numarasını belirten bir talebin URI'sini alır.

Locality

Bir varlığın bulunduğu yerel ayarı belirten bir talebin URI'sini alır.

MobilePhone

Bir varlığın cep telefonu numarasını belirten talebin URI'sini alır.

Name

Bir varlığın adını belirten bir talebin URI'sini alır.

NameIdentifier

Bir varlığın adını belirten bir talebin URI'sini alır.

OtherPhone

Bir varlığın alternatif telefon numarasını belirten talebin URI'sini alır.

PostalCode

Bir varlığın posta kodunu belirten bir talebin URI'sini alır.

PPID

Bir varlığın özel kişisel tanımlayıcısını (PPI) belirten bir talebin URI'sini alır.

Rsa

RSA anahtarını belirten bir talebin URI'sini alır.

Sid

Güvenlik tanımlayıcısı (SID) belirten bir talebin URI'sini alır.

Spn

Hizmet asıl adı (SPN) talebi belirten bir talebin URI'sini alır.

StateOrProvince

Bir varlığın bulunduğu durumu veya eyaleti belirten talebin URI'sini alır.

StreetAddress

Bir varlığın açık adresini belirten bir talebin URI'sini alır.

Surname

Bir varlığın soyadını belirten bir talebin URI'sini alır.

System

Sistem varlığını tanımlayan bir talebin URI'sini alır.

Thumbprint

Parmak izini belirten bir talebin URI'sini alır.

Upn

Kullanıcı asıl adını (UPN) belirten bir talebin URI'sini alır.

Uri

URI belirten bir talebin URI'sini alır.

Webpage

Bir varlığın Web sayfasını belirten bir talebin URI'sini alır.

X500DistinguishedName

X.509 sertifikasının ayırt edici ad talebi için URI'yi içeren dizeyi alır.

Şunlara uygulanır