Share via


ClaimTypes Classe

Definizione

Rappresenta i tipi predefiniti di attestazioni che può richiedere un'entità. La classe non può essere ereditata.

public ref class ClaimTypes abstract sealed
public static class ClaimTypes
type ClaimTypes = class
Public Class ClaimTypes
Ereditarietà
ClaimTypes

Esempio


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

Commenti

Utilizzare la classe ClaimTypes per cercare un particolare tipo di attestazione in una classe ClaimSet o per creare un'attestazione. Per cercare un particolare tipo di attestazione in una classe ClaimSet, utilizzare il metodo FindClaims(String, String) e utilizzare le proprietà di questa classe per specificare il tipo di attestazione per il parametro claimType. Quando il costruttore per la classe Claim viene utilizzato per creare una nuova attestazione, utilizzare le proprietà della classe ClaimTypes per specificare il parametro claimType. Per molti dei tipi di attestazione, la classe Claim ha proprietà statiche che restituiscono un'attestazione di un tipo specifico. Il metodo CreateHashClaim(Byte[]), ad esempio, restituisce un'attestazione utilizzando il tipo di attestazione Hash.

Proprietà

Anonymous

Ottiene l'URI per un'attestazione che specifica l'utente anonimo.

Authentication

Ottiene l'URI per un'attestazione che specifica i dettagli sull'autenticazione di un'identità.

AuthorizationDecision

Ottiene l'URI per un'attestazione che specifica la decisione di autorizzazione su un'entità.

Country

Ottiene l'URI per un'attestazione che specifica il paese/regione in cui risiede un'entità.

DateOfBirth

Ottiene l'URI per un'attestazione che specifica la data di nascita di un'entità.

DenyOnlySid

Ottiene l'URI per un'attestazione che specifica un ID di sicurezza (SID) di sola negazione per un'entità.

Dns

Ottiene l'URI per un'attestazione che specifica il nome DNS associato al nome del computer o al nome alternativo dell'oggetto o dell'emittente di un certificato X.509.

Email

Ottiene l'URI per un'attestazione che specifica l'indirizzo di posta elettronica di un'entità.

Gender

Ottiene l'URI per un'attestazione che specifica il genere di un'entità.

GivenName

Ottiene l'URI per un'attestazione che specifica il nome assegnato a un'entità.

Hash

Ottiene l'URI per un'attestazione che specifica un valore hash.

HomePhone

Ottiene l'URI per un'attestazione che specifica il numero di telefono dell'abitazione di un'entità.

Locality

Ottiene l'URI per un'attestazione che specifica le impostazioni locali relative a un'entità.

MobilePhone

Ottiene l'URI per un'attestazione che specifica il numero di telefono cellulare di un'entità.

Name

Ottiene l'URI per un'attestazione che specifica il nome di un'entità.

NameIdentifier

Ottiene l'URI per un'attestazione che specifica il nome di un'entità.

OtherPhone

Ottiene l'URI per un'attestazione che specifica il numero di telefono alternativo di un'entità.

PostalCode

Ottiene l'URI per un'attestazione che specifica il codice postale di un'entità.

PPID

Ottiene l'URI per un'attestazione che specifica l'ID personale privato (PPID) di un'entità.

Rsa

Ottiene l'URI per un'attestazione che specifica una chiave RSA.

Sid

Ottiene l'URI per un'attestazione che specifica un ID di sicurezza (SID).

Spn

Ottiene l'URI per un'attestazione che specifica un'attestazione del nome dell'entità servizio (SPN).

StateOrProvince

Ottiene l'URI per un'attestazione che specifica lo stato o la provincia in cui risiede un'entità.

StreetAddress

Ottiene l'URI per un'attestazione che specifica la via e il numero civico di un'entità.

Surname

Ottiene l'URI per un'attestazione che specifica il cognome di un'entità.

System

Ottiene l'URI per un'attestazione che identifica l'entità di sistema.

Thumbprint

Ottiene l'URI per un'attestazione che specifica un'identificazione digitale.

Upn

Ottiene l'URI per un'attestazione che specifica un nome di entità utente (UPN).

Uri

Ottiene l'URI per un'attestazione che specifica un URI.

Webpage

Ottiene l'URI per un'attestazione che specifica la pagina Web di un'entità.

X500DistinguishedName

Ottiene la stringa che contiene l'URI per un'attestazione nome distinto di un certificato X.509.

Si applica a