ClaimTypes 클래스

정의

엔터티가 요구할 수 있는 미리 정의된 형식의 클레임을 나타냅니다. 이 클래스는 상속될 수 없습니다.

public ref class ClaimTypes abstract sealed
public static class ClaimTypes
type ClaimTypes = class
Public Class ClaimTypes
상속
ClaimTypes

예제


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

설명

클래스를 ClaimTypes 사용하여 에서 ClaimSet 특정 유형의 클레임을 검색하거나 클레임을 만듭니다. 에서 ClaimSet특정 유형의 클레임을 검색하려면 메서드를 FindClaims(String, String) 사용하고 이 클래스의 속성을 사용하여 매개 변수에 대한 클레임 형식을 claimType 지정합니다. 클래스의 생성자를 Claim 사용하여 새 클레임을 만들 때 클래스의 ClaimTypes 속성을 사용하여 매개 변수를 지정합니다 claimType . 많은 클레임 형식의 경우 클래스에는 특정 형식 Claim 의 클레임을 반환하는 정적 속성이 있습니다. instance 경우 메서드는 CreateHashClaim(Byte[]) 클레임 유형을 사용하여 클레임을 Hash 반환합니다.

속성

Anonymous

익명 사용자를 지정하는 클레임에 대한 URI를 가져옵니다.

Authentication

ID가 인증되는지 여부에 대한 정보를 지정하는 클레임에 대한 URI를 가져옵니다.

AuthorizationDecision

엔터티에 대한 권한 부여 결정을 지정하는 클레임에 대한 URI를 가져옵니다.

Country

엔터티가 있는 국가/지역을 지정하는 클레임에 대한 URI를 가져옵니다.

DateOfBirth

엔터티의 생년월일을 지정하는 클레임에 대한 URI를 가져옵니다.

DenyOnlySid

엔터티의 거부 전용 SID(보안 식별자)를 지정하는 클레임에 대한 URI를 가져옵니다.

Dns

컴퓨터 이름이나 X.509 인증서의 발급자 또는 주체의 대체 이름과 연결된 DNS 이름을 지정하는 클레임에 대한 URI를 가져옵니다.

Email

엔터티의 전자 메일 주소를 지정하는 클레임에 대한 URI를 가져옵니다.

Gender

엔터티의 성별을 지정하는 클레임에 대한 URI를 가져옵니다.

GivenName

엔터티의 이름을 지정하는 클레임에 대한 URI를 가져옵니다.

Hash

해시 값을 지정하는 클레임에 대한 URI를 가져옵니다.

HomePhone

엔터티의 집 전화 번호를 지정하는 클레임에 대한 URI를 가져옵니다.

Locality

엔터티가 있는 로캘을 지정하는 클레임에 대한 URI를 가져옵니다.

MobilePhone

엔터티의 휴대폰 번호를 지정하는 클레임에 대한 URI를 가져옵니다.

Name

엔터티의 이름을 지정하는 클레임에 대한 URI를 가져옵니다.

NameIdentifier

엔터티의 이름을 지정하는 클레임에 대한 URI를 가져옵니다.

OtherPhone

엔터티의 대체 전화 번호를 지정하는 클레임에 대한 URI를 가져옵니다.

PostalCode

엔터티의 우편 번호를 지정하는 클레임에 대한 URI를 가져옵니다.

PPID

엔터티의 PPI(Private Personal Identifier)를 지정하는 클레임에 대한 URI를 가져옵니다.

Rsa

RSA 키를 지정하는 클레임에 대한 URI를 가져옵니다.

Sid

SID(보안 식별자)를 지정하는 클레임에 대한 URI를 가져옵니다.

Spn

SPN(서비스 사용자 이름) 클레임을 지정하는 클레임에 대한 URI를 가져옵니다.

StateOrProvince

엔터티가 있는 시/도를 지정하는 클레임에 대한 URI를 가져옵니다.

StreetAddress

엔터티의 주소를 지정하는 클레임에 대한 URI를 가져옵니다.

Surname

엔터티의 성을 지정하는 클레임에 대한 URI를 가져옵니다.

System

시스템 엔터티를 식별하는 클레임에 대한 URI를 가져옵니다.

Thumbprint

지문을 지정하는 클레임에 대한 URI를 가져옵니다.

Upn

UPN(User Principal Name)을 지정하는 클레임에 대한 URI를 가져옵니다.

Uri

URI를 지정하는 클레임에 대한 URI를 가져옵니다.

Webpage

엔터티의 웹 페이지를 지정하는 클레임에 대한 URI를 가져옵니다.

X500DistinguishedName

X.509 인증서의 고유 이름 클레임에 대한 URI가 포함된 문자열을 가져옵니다.

적용 대상