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 中搜尋特定型別的宣告或建立宣告。 若要在 ClaimSetFindClaims(String, String)中搜尋特定型別的宣告,請使用 claimType 方法,並且使用這個類別的屬性指定 參數的宣告型別。 當 Claim 類別的建構函式是用來建立新宣告時,可使用 ClaimTypes 類別的屬性來指定 claimType 參數。 對於許多宣告型別而言,Claim 類別的靜態屬性會傳回特定型別的宣告。 例如,CreateHashClaim(Byte[]) 方法會使用 Hash 宣告型別傳回宣告。

屬性

Anonymous

取得指定匿名使用者之宣告的 URI。

Authentication

取得宣告的 URI,這個宣告會明確指出有關是否已驗證識別的詳細資料。

AuthorizationDecision

取得宣告的 URI,這個宣告會明確指出針對實體的授權決策。

Country

取得指定實體所在國家/地區之宣告的 URI。

DateOfBirth

取得指定實體生日之宣告的 URI。

DenyOnlySid

取得宣告的 URI,這個宣告會指定實體的指定禁用 (Deny-Only) 安全識別項 (SID)。

Dns

取得指定 DNS 名稱之宣告的 URI,該 DNS 名稱與電腦名稱或 X.509 憑證主體或發行者之替代名稱相關聯。

Email

取得指定實體電子郵件地址之宣告的 URI。

Gender

取得指定實體性別之宣告的 URI。

GivenName

取得宣告的 URI,這個宣告會指定實體的指定名稱。

Hash

取得指定雜湊值之宣告的 URI。

HomePhone

取得指定實體住家電話號碼之宣告的 URI。

Locality

取得指定實體所在地區設定之宣告的 URI。

MobilePhone

取得指定實體行動電話號碼之宣告的 URI。

Name

取得指定實體名稱之宣告的 URI。

NameIdentifier

取得指定實體名稱之宣告的 URI。

OtherPhone

取得指定實體替代電話號碼之宣告的 URI。

PostalCode

取得指定實體郵遞區號之宣告的 URI。

PPID

取得指定實體私密個人識別碼 (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

取得指定實體網頁之宣告的 URI。

X500DistinguishedName

取得字串,這個字串包含 X.509 憑證之辨別名稱宣告的 URI。

適用於