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
要素のセキュリティ ID (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> <!-- 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>