HOW TO:從 c2WTS 要求權杖

下列程式碼範例示範如何從 要求權仗,並使用該權仗模擬使用者。 如需相關資訊,請參閱 Claims to Windows Token Service (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. }

系統管理員必須以允許的呼叫者清單來設定 ,而允許的呼叫者清單也就是位於 WIF 安裝資料夾之版本資料夾內的設定檔 c2wtshost.exe.config 中,Microsoft.IdentityModel 區段內 allowedCallers 元素中的安全性識別碼 (SID) 清單。 例如,如果您將 WIF 3.5 版安裝到 C:\Program Files,則 c2wtshost.exe.config 檔位於 C:\Program Files\Windows Identity Foundation\v3.5 資料夾中。 範例如下:

<?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> <!-- 根據預設,呼叫者不得使用 Claims to Windows Token Service。 請在下方新增您要允許的身分識別。 --> <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>