Share via


Procédure : Demander un jeton de c2WTS

L'exemple de code suivant indique comment demander un jeton à et l'utiliser pour emprunter l'identité de l'utilisateur. Pour plus d'informations, consultez Vue d'ensemble des revendications au service d'émission de jeton Windows (c2WTS).

// Get the current identity and extract the UPN claim. IClaimsIdentity identity = ( ClaimsIdentity )Thread.CurrentPrincipal.Identity; string upn = null; foreach ( Claim claim in identity.Claims ) { if ( StringComparer.Ordinal.Equals( System.IdentityModel.Claims.ClaimTypes.Upn, claim.ClaimType ) ) { upn = claim.Value; } }

// Perform the UPN logon through the c2WTS. WindowsIdentity windowsIdentity = null; if ( !String.IsNullOrEmpty( upn ) ) { try { windowsIdentity = S4UClient.UpnLogon( upn ); } catch ( SecurityAccessDeniedException ) { Console.WriteLine( "Could not map the upn claim to a valid windows identity." ); return; } } else { throw new Exception( "No UPN claim found" ); }

using ( WindowsImpersonationContext ctxt = windowsIdentity.Impersonate() ) { // Access the resource. }

Un administrateur doit configurer avec une liste d'appelants autorisés, c'est-à-dire la liste d'identificateurs de sécurité (SID) de l'élément allowedCallers de la section Microsoft.IdentityModel du fichier de configuration c2wtshost.exe.config, situé dans le dossier de version du dossier d'installation WIF. Par exemple, si vous avez installé WIF version 3.5 dans C:\Program Files, le fichier c2wtshost.exe.config se trouve dans le dossier C:\Program Files\Windows Identity Foundation\v3.5. Voici un exemple :

<?xml version="1.0"?>

<configuration> <configSections> <section name="windowsTokenService" type="Microsoft.IdentityModel.WindowsTokenService.Configuration.WindowsTokenServiceSection, Microsoft.IdentityModel.WindowsTokenService, Version=0.6.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </configSections>

  <windowsTokenService> <!-- By default no callers are allowed to use the Claims to Windows Token Service. Add the identities you wish to allow below. --> <allowedCallers> <clear/> <!-- <add value="NT AUTHORITY\Network Service" /> --> <!-- <add value="NT AUTHORITY\Local Service" /> --> <!-- <add value="NT AUTHORITY\System" /> --> <!-- <add value="NT AUTHORITY\Authenticated Users" /> --> </allowedCallers> </windowsTokenService> </configuration>