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 クラスには、特定の種類のクレームを返す静的なプロパティがあります。 たとえば、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

エンティティの Private Personal Identifier (PPI) を指定するクレームの URI を取得します。

Rsa

RSA キーを指定するクレームの URI を取得します。

Sid

セキュリティ識別子 (SID) を指定するクレームの URI を取得します。

Spn

サービス プリンシパル名 (SPN) クレームを指定するクレームの URI を取得します。

StateOrProvince

エンティティが存在する都道府県を指定するクレームの URI を取得します。

StreetAddress

エンティティの番地を指定するクレームの URI を取得します。

Surname

エンティティの姓を指定するクレームの URI を取得します。

System

システム エンティティを示すクレームの URI を取得します。

Thumbprint

拇印を指定するクレームの URI を取得します。

Upn

ユーザー プリンシパル名 (UPN) を指定するクレームの URI を取得します。

Uri

URI を指定するクレームの URI を取得します。

Webpage

エンティティの Web ページを指定するクレームの URI を取得します。

X500DistinguishedName

X.509 証明書の識別名クレームの URI を格納した文字列を取得します。

適用対象