Share via


ClaimTypes Třída

Definice

Představuje předdefinované typy deklarací identity, které může entita deklarovat. Tato třída se nemůže dědit.

public ref class ClaimTypes abstract sealed
public static class ClaimTypes
type ClaimTypes = class
Public Class ClaimTypes
Dědičnost
ClaimTypes

Příklady


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

Poznámky

ClaimTypes Pomocí třídy můžete vyhledat konkrétní typ deklarace identity v objektu ClaimSet nebo vytvořit deklaraci identity. Pokud chcete vyhledat konkrétní typ deklarace identity v objektu ClaimSet, použijte metodu FindClaims(String, String) a vlastnosti této třídy k určení typu deklarace identity parametru claimType . Pokud konstruktor pro Claim třídu slouží k vytvoření nové deklarace identity, pomocí vlastností ClaimTypes třídy zadejte claimType parametr . Pro mnoho typů Claim deklarací identity má třída statické vlastnosti, které vracejí deklaraci identity určitého typu. Metoda například CreateHashClaim(Byte[]) vrátí deklaraci identity pomocí Hash typu deklarace identity.

Vlastnosti

Anonymous

Získá identifikátor URI pro deklaraci identity, která určuje anonymního uživatele.

Authentication

Získá identifikátor URI pro deklaraci identity, která určuje podrobnosti o tom, zda je identita ověřena.

AuthorizationDecision

Získá identifikátor URI pro deklaraci identity, která určuje rozhodnutí o autorizaci entity.

Country

Získá identifikátor URI deklarace identity, který určuje zemi nebo oblast, ve které se nachází entita.

DateOfBirth

Získá identifikátor URI pro deklaraci identity, která určuje datum narození entity.

DenyOnlySid

Získá identifikátor URI pro deklaraci identity, který určuje identifikátor zabezpečení (SID) pouze odepřít pro entitu.

Dns

Získá identifikátor URI pro deklaraci identity, která určuje název DNS přidružený k názvu počítače nebo s alternativním názvem subjektu nebo vystavitele certifikátu X.509.

Email

Získá identifikátor URI pro deklaraci identity, která určuje e-mailovou adresu entity.

Gender

Získá identifikátor URI pro deklaraci identity, která určuje pohlaví entity.

GivenName

Získá identifikátor URI pro deklaraci identity, která určuje daný název entity.

Hash

Získá identifikátor URI deklarace identity, která určuje hodnotu hash.

HomePhone

Získá identifikátor URI pro deklaraci identity, která určuje telefonní číslo domů entity.

Locality

Získá identifikátor URI pro deklaraci identity, která určuje národní prostředí, ve kterém se nachází entita.

MobilePhone

Získá identifikátor URI pro deklaraci identity, která určuje číslo mobilního telefonu entity.

Name

Získá identifikátor URI deklarace identity, který určuje název entity.

NameIdentifier

Získá identifikátor URI deklarace identity, který určuje název entity.

OtherPhone

Získá identifikátor URI deklarace identity, který určuje alternativní telefonní číslo entity.

PostalCode

Získá identifikátor URI pro deklaraci identity, která určuje PSČ entity.

PPID

Získá identifikátor URI pro deklaraci identity, která určuje soukromý osobní identifikátor (PPI) entity.

Rsa

Získá identifikátor URI pro deklaraci identity, která určuje klíč RSA.

Sid

Získá identifikátor URI pro deklaraci identity, která určuje identifikátor zabezpečení (SID).

Spn

Získá identifikátor URI pro deklaraci identity, která určuje hlavní název služby (SPN) deklarace identity.

StateOrProvince

Získá identifikátor URI pro deklaraci identity, která určuje stát nebo provincii, ve které se nachází entita.

StreetAddress

Získá identifikátor URI pro deklaraci identity, která určuje poštovní adresu entity.

Surname

Získá identifikátor URI pro deklaraci identity, která určuje příjmení entity.

System

Získá identifikátor URI deklarace identity, která identifikuje entitu systému.

Thumbprint

Získá identifikátor URI pro deklaraci identity, která určuje kryptografický otisk.

Upn

Získá identifikátor URI pro deklaraci identity, která určuje hlavní název uživatele (UPN).

Uri

Získá identifikátor URI pro deklaraci identity, která určuje identifikátor URI.

Webpage

Získá identifikátor URI pro deklaraci identity, která určuje webovou stránku entity.

X500DistinguishedName

Získá řetězec, který obsahuje identifikátor URI pro rozlišující název deklarace certifikátu X.509.

Platí pro