ClaimTypes Classe

Définition

Représente les types prédéfinis des revendications qu'une entité peut réclamer. Cette classe ne peut pas être héritée.

public ref class ClaimTypes abstract sealed
public static class ClaimTypes
type ClaimTypes = class
Public Class ClaimTypes
Héritage
ClaimTypes

Exemples


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

Remarques

Utilisez la classe ClaimTypes pour rechercher un type particulier de revendication dans un ClaimSet ou pour créer une revendication. Pour rechercher un type particulier de revendication dans un ClaimSet, utilisez la méthode FindClaims(String, String) et utilisez les propriétés de cette classe pour spécifier le type de revendication du paramètre claimType. Lorsque le constructeur pour la classe Claim est utilisé pour créer une revendication, utilisez les propriétés de la classe ClaimTypes pour spécifier le paramètre claimType. Pour un grand nombre des types de revendications, la classe Claim possède des propriétés statiques qui retournent une revendication d'un type spécifique. Par exemple, la méthode CreateHashClaim(Byte[]) retourne une revendication à l'aide du type de revendication Hash.

Propriétés

Anonymous

Obtient l'URI pour une revendication qui spécifie l'utilisateur anonyme.

Authentication

Obtient l'URI pour une revendication qui spécifie des détails sur l'authentification d'une identité.

AuthorizationDecision

Obtient l'URI pour une revendication qui spécifie une décision d'autorisation sur une entité.

Country

Obtient l'URI pour une revendication qui spécifie le pays/région dans lequel une entité réside.

DateOfBirth

Obtient l'URI pour une revendication qui spécifie la date de naissance d'une entité.

DenyOnlySid

Obtient l'URI pour une revendication qui spécifie un identificateur de sécurité (SID) en refus seul pour une entité.

Dns

Obtient l'URI pour une revendication qui spécifie le nom DNS associé au nom de l'ordinateur ou au nom alternatif de l'objet ou de l'émetteur d'un certificat X509.

Email

Obtient l'URI pour une revendication qui spécifie l'adresse de messagerie d'une entité.

Gender

Obtient l'URI pour une revendication qui spécifie le genre d'une entité.

GivenName

Obtient l'URI pour une revendication qui spécifie le nom donné d'une entité.

Hash

Obtient l'URI pour une revendication qui spécifie une valeur de hachage.

HomePhone

Obtient l'URI pour une revendication qui spécifie le numéro de téléphone personnel d'une entité.

Locality

Reçoit l'URI pour une revendication qui spécifie les paramètres régionaux d'une entité.

MobilePhone

Obtient l'URI pour une revendication qui spécifie le numéro de téléphone mobile d'une entité.

Name

Obtient l'URI pour une revendication qui spécifie le nom d'une entité.

NameIdentifier

Obtient l'URI pour une revendication qui spécifie le nom d'une entité.

OtherPhone

Obtient l'URI pour une revendication qui spécifie un numéro de téléphone différent d'une entité.

PostalCode

Obtient l'URI pour une revendication qui spécifie le code postal d'une entité.

PPID

Reçoit l'URI pour une revendication qui spécifie l'identifiant personnel privé (PPID, private personal identifier) d'une entité.

Rsa

Obtient l'URI pour une revendication qui spécifie une clé RSA.

Sid

Obtient l'URI pour une revendication qui spécifie un identificateur de sécurité (SID, security identifier).

Spn

Obtient l'URI pour une revendication qui spécifie une revendication de nom de principal du service (SPN, service principal name).

StateOrProvince

Obtient l'URI pour une revendication qui spécifie l'état ou la province d'une entité.

StreetAddress

Obtient l'URI pour une revendication qui spécifie l'adresse postale d'une entité.

Surname

Obtient l'URI pour une revendication qui spécifie le nom de famille d'une entité.

System

Obtient l'URI pour une revendication qui identifie l'entité système.

Thumbprint

Obtient l'URI pour une revendication qui spécifie une empreinte numérique.

Upn

Obtient l'URI pour une revendication qui spécifie un nom d'utilisateur principal (UPN, user principal name).

Uri

Obtient l'URI pour une revendication qui spécifie un URI.

Webpage

Obtient l’URI pour une revendication qui spécifie la page web d’une entité.

X500DistinguishedName

Obtient la chaîne qui contient l'URI pour une revendication de nom unique d'un certificat X.509.

S’applique à