此範例示範如何設定 WSHttpBinding 繫結,以透過 Windows 驗證來使用訊息層級安全性。 此範例是以 用戶入門為基礎。 在此範例中,服務裝載於 Internet Information Services (IIS),而用戶端是控制台應用程式 (.exe)。
備註
此範例的安裝程式和建置指示位於本主題結尾。
wsHttpBinding< 的預設安全性>是使用 Windows 驗證的訊息安全性。 這個範例中的組態檔會明確將 mode 屬性設定<為 > ,並將 屬性設定為 Message。 這些值是這個系結的預設值,但已明確設定,如下列範例組態所示,以示範其用法。
<bindings>
<wsHttpBinding>
<binding>
<security mode="Message">
<message clientCredentialType="Windows"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
用戶端端點組態是由服務端點、系結和合約的絕對位址所組成。 用戶端系結是使用適當的 securityMode 和 authenticationMode來設定。
<system.serviceModel>
<client>
<endpoint address=
"http://localhost/servicemodelsamples/service.svc"
binding="wsHttpBinding"
bindingConfiguration="Binding1"
contract="Microsoft.ServiceModel.Samples.ICalculator" />
</client>
<bindings>
<wsHttpBinding>
<!-- The default security for the WSHttpBinding is -->
<!-- Message security using Windows authentication. -->
<!-- This configuration explicitly defines the security mode -->
<!-- as Message and the clientCredentialType as Windows -->
<!-- for demonstration purposes. -->
<binding name="Binding1">
<security mode="Message">
<message clientCredentialType="Windows"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
</system.serviceModel>
已修改服務原始程式碼,以示範 如何使用 ServiceSecurityContext 來存取呼叫者的身分識別。
public string GetCallerIdentity()
{
// The Windows identity of the caller can be accessed on the ServiceSecurityContext.WindowsIdentity.
return OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name;
}
當您執行範例時,作業要求和回應會顯示在用戶端控制台視窗中。 第一個稱為 - GetCallerIdentity 的方法 - 會將呼叫端身分識別的名稱傳回用戶端。 在主控台視窗中按 ENTER 鍵以關閉用戶端。
要設定、建置和執行範例,請執行以下步驟:
請確定您已針對 Windows Communication Foundation 範例 執行One-Time 安裝程式。
若要建置解決方案的 C# 或 Visual Basic .NET 版本,請遵循建置 Windows Communication Foundation 範例 中中的指示。
若要在單一或跨計算機組態中執行範例,請遵循 執行 Windows Communication Foundation 範例中的指示。