模擬範例示範如何在服務模擬呼叫端應用程式,讓服務可以代表呼叫端存取系統資源。
此範例是以 自行托管 範例為基礎。 服務和用戶端組態檔與 自我裝載 範例的組態檔相同。
備註
此範例的安裝程式和建置指示位於本主題結尾。
已修改服務程序代碼,讓 Add 服務上的方法使用 OperationBehaviorAttribute 模擬呼叫端,如下列範例程式代碼所示。
[OperationBehavior(Impersonation = ImpersonationOption.Required)]
public double Add(double n1, double n2)
{
double result = n1 + n2;
Console.WriteLine("Received Add({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
DisplayIdentityInformation();
return result;
}
如此一來,執行線程的安全性內容會切換為模擬呼叫端,再輸入 Add 方法,並在結束方法時還原。
DisplayIdentityInformation下列範例程式代碼中顯示的方法是顯示呼叫端身分識別的公用程式函式。
static void DisplayIdentityInformation()
{
Console.WriteLine("\t\tThread Identity :{0}",
WindowsIdentity.GetCurrent().Name);
Console.WriteLine("\t\tThread Identity level :{0}",
WindowsIdentity.GetCurrent().ImpersonationLevel);
Console.WriteLine("\t\thToken :{0}",
WindowsIdentity.GetCurrent().Token.ToString());
return;
}
Subtract服務上的 方法會使用命令式呼叫模擬呼叫端,如下列範例程式代碼所示。
public double Subtract(double n1, double n2)
{
double result = n1 - n2;
Console.WriteLine("Received Subtract({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
Console.WriteLine("Before impersonating");
DisplayIdentityInformation();
if (ServiceSecurityContext.Current.WindowsIdentity.ImpersonationLevel == TokenImpersonationLevel.Impersonation ||
ServiceSecurityContext.Current.WindowsIdentity.ImpersonationLevel == TokenImpersonationLevel.Delegation)
{
// Impersonate.
using (ServiceSecurityContext.Current.WindowsIdentity.Impersonate())
{
// Make a system call in the caller's context and ACLs
// on the system resource are enforced in the caller's context.
Console.WriteLine("Impersonating the caller imperatively");
DisplayIdentityInformation();
}
}
else
{
Console.WriteLine("ImpersonationLevel is not high enough to perform this operation.");
}
Console.WriteLine("After reverting");
DisplayIdentityInformation();
return result;
}
請注意,在此情況下,呼叫者在整個呼叫過程中不會被完全模擬,而僅在部分呼叫過程中被替代身份。 一般而言,模擬最小範圍比模擬整個作業更佳。
其他方法不會冒充呼叫者。
已修改客戶端程式代碼,將模擬層級設定為 Impersonation。 用戶端會使用 TokenImpersonationLevel 列舉,指定要由服務使用的模擬層級。 列舉支援下列值:None、Anonymous、 IdentificationImpersonation 和 Delegation。 若要在使用 Windows ACL 保護的本機電腦上存取系統資源時執行存取檢查,模擬層級必須設定為 Impersonation,如下列範例程式代碼所示。
// Create a client with given client endpoint configuration
CalculatorClient client = new CalculatorClient();
client.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
當您執行範例時,作業要求和回應會顯示在服務和用戶端控制台視窗中。 在每個主控台視窗中按 ENTER 鍵,關閉服務和用戶端。
備註
服務必須在系統管理帳戶下執行,或其執行下的帳戶必須獲得許可權,才能向 HTTP 層註冊 http://localhost:8000/ServiceModelSamples URI。 您可以使用 Httpcfg.exe 工具來設定命名空間保留來授與這類許可權。
備註
在執行 Windows Server 2003 的電腦上,只有在 Host.exe 應用程式具有模擬許可權時,才支持模擬。 (根據預設,只有系統管理員擁有此許可權。)若要將此權限新增至服務運行的帳戶,請前往 系統管理工具,開啟 本機安全性原則,開啟 本機原則,按一下 使用者權限指派,然後選取 驗證後模擬用戶端,按兩下 屬性 以新增使用者或群組。
要設定、建置和執行範例,請執行以下步驟:
請確定您已針對 Windows Communication Foundation 範例 執行One-Time 安裝程式。
若要建置解決方案的 C# 或 Visual Basic .NET 版本,請遵循建置 Windows Communication Foundation 範例 中中的指示。
若要在單一或跨計算機組態中執行範例,請遵循執行 Windows Communication Foundation 範例 中的指示。
為了展示服務如何偽裝為呼叫者,請在不與服務相同的帳戶下執行用戶端。 若要這樣做,請在命令提示字元中輸入:
runas /user:<machine-name>\<user-name> client.exe然後系統會提示您輸入密碼。 輸入您先前指定之帳戶的密碼。
當您執行用戶端時,請注意執行不同憑證前後的識別。