ClaimTypes Třída
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Představuje předdefinované typy deklarací identity, které může entita deklarovat. Tuto třídu nelze zdě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 ClaimSet deklarací identity nebo vytvořit deklaraci identity. Chcete-li vyhledat konkrétní typ deklarace identity v aplikaci ClaimSet, použijte metodu FindClaims(String, String) a pomocí vlastností této třídy zadejte typ deklarace pro claimType parametr. Pokud se konstruktor třídy Claim používá k vytvoření nové deklarace identity, použijte vlastnosti ClaimTypes třídy k určení parametru claimType . U mnoha typů Claim deklarací identity má třída statické vlastnosti, které vracejí deklaraci identity konkrétního typu. Například CreateHashClaim(Byte[]) metoda vrátí deklaraci identity pomocí Hash typu deklarace identity.
Vlastnosti
| Name | Description |
|---|---|
| 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 u entity. |
| Country |
Získá identifikátor URI pro deklaraci identity, která určuje zemi nebo oblast, ve které se entita nachází. |
| 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í jen pro odepření (SID) 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 alternativní název subjektu nebo vystavitele certifikátu X.509. |
|
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 zadaný název entity. |
| Hash |
Získá identifikátor URI pro deklaraci 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 entita nachází. |
| MobilePhone |
Získá identifikátor URI pro deklaraci identity, která určuje číslo mobilního telefonu entity. |
| Name |
Získá identifikátor URI pro deklaraci identity, která určuje název entity. |
| NameIdentifier |
Získá identifikátor URI pro deklaraci identity, která určuje název entity. |
| OtherPhone |
Získá identifikátor URI pro deklaraci 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 privátní 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 deklaraci hlavního názvu služby (SPN). |
| StateOrProvince |
Získá identifikátor URI pro deklaraci identity, která určuje stát nebo provincii, ve které se entita nachází. |
| StreetAddress |
Získá identifikátor URI pro deklaraci identity, která určuje adresu ulice entity. |
| Surname |
Získá identifikátor URI pro deklaraci identity, která určuje příjmení entity. |
| System |
Získá identifikátor URI pro deklaraci identity, která identifikuje systémovou entitu. |
| 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 identity certifikátu X.509. |