ClaimTypes Klasa

Definicja

Reprezentuje wstępnie zdefiniowane typy oświadczeń, które jednostka może oświadczeń. Klasa ta nie może być dziedziczona.

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

Przykłady


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

Uwagi

ClaimTypes Użyj klasy , aby wyszukać określony typ oświadczenia w obiekcie ClaimSet lub, aby utworzyć oświadczenie. Aby wyszukać określony typ oświadczenia w ClaimSetobiekcie , użyj FindClaims(String, String) metody i użyj właściwości tej klasy, aby określić typ oświadczenia dla parametru claimType . Gdy konstruktor klasy Claim jest używany do tworzenia nowego oświadczenia, użyj właściwości ClaimTypes klasy, aby określić claimType parametr . W przypadku wielu typów Claim oświadczeń klasa ma właściwości statyczne, które zwracają oświadczenie określonego typu. Na przykład CreateHashClaim(Byte[]) metoda zwraca oświadczenie przy użyciu Hash typu oświadczenia.

Właściwości

Anonymous

Pobiera identyfikator URI oświadczenia, który określa użytkownika anonimowego.

Authentication

Pobiera identyfikator URI oświadczenia, który określa szczegóły dotyczące tego, czy tożsamość jest uwierzytelniona.

AuthorizationDecision

Pobiera identyfikator URI oświadczenia, który określa decyzję autoryzacji dla jednostki.

Country

Pobiera identyfikator URI oświadczenia, który określa kraj/region, w którym znajduje się jednostka.

DateOfBirth

Pobiera identyfikator URI oświadczenia, który określa datę urodzenia jednostki.

DenyOnlySid

Pobiera identyfikator URI oświadczenia, który określa identyfikator zabezpieczeń tylko odmów (SID) dla jednostki.

Dns

Pobiera identyfikator URI oświadczenia, który określa nazwę DNS skojarzona z nazwą komputera lub alternatywną nazwą podmiotu lub wystawcy certyfikatu X.509.

Email

Pobiera identyfikator URI oświadczenia, który określa adres e-mail jednostki.

Gender

Pobiera identyfikator URI oświadczenia, który określa płeć jednostki.

GivenName

Pobiera identyfikator URI oświadczenia, który określa daną nazwę jednostki.

Hash

Pobiera identyfikator URI oświadczenia, który określa wartość skrótu.

HomePhone

Pobiera identyfikator URI oświadczenia, który określa numer telefonu macierzystego jednostki.

Locality

Pobiera identyfikator URI oświadczenia, który określa ustawienia regionalne, w których znajduje się jednostka.

MobilePhone

Pobiera identyfikator URI oświadczenia, który określa numer telefonu komórkowego jednostki.

Name

Pobiera identyfikator URI oświadczenia, który określa nazwę jednostki.

NameIdentifier

Pobiera identyfikator URI oświadczenia, który określa nazwę jednostki.

OtherPhone

Pobiera identyfikator URI oświadczenia, który określa alternatywny numer telefonu jednostki.

PostalCode

Pobiera identyfikator URI oświadczenia, który określa kod pocztowy jednostki.

PPID

Pobiera identyfikator URI oświadczenia, który określa prywatny identyfikator osobisty (PPI) jednostki.

Rsa

Pobiera identyfikator URI oświadczenia, który określa klucz RSA.

Sid

Pobiera identyfikator URI oświadczenia, który określa identyfikator zabezpieczeń (SID).

Spn

Pobiera identyfikator URI oświadczenia, który określa oświadczenie głównej nazwy usługi (SPN).

StateOrProvince

Pobiera identyfikator URI oświadczenia, który określa stan lub prowincję, w której znajduje się jednostka.

StreetAddress

Pobiera identyfikator URI oświadczenia, który określa adres ulicy jednostki.

Surname

Pobiera identyfikator URI oświadczenia, który określa nazwisko jednostki.

System

Pobiera identyfikator URI oświadczenia identyfikującego jednostkę systemową.

Thumbprint

Pobiera identyfikator URI oświadczenia, który określa odcisk palca.

Upn

Pobiera identyfikator URI oświadczenia, który określa główną nazwę użytkownika (UPN).

Uri

Pobiera identyfikator URI oświadczenia, który określa identyfikator URI.

Webpage

Pobiera identyfikator URI oświadczenia, który określa stronę sieci Web jednostki.

X500DistinguishedName

Pobiera ciąg zawierający identyfikator URI dla oświadczenia nazwy wyróżniającej certyfikatu X.509.

Dotyczy