此示例演示如何配置 WSHttpBinding 绑定,以使用 Windows 身份验证的消息级安全性。 此示例基于入门指南。 在此示例中,该服务托管在 Internet Information Services (IIS)中,客户端是控制台应用程序(.exe)。
注释
本示例的设置过程和生成说明位于本主题末尾。
<wsHttpBinding> 的默认安全性是消息安全性,并使用 Windows 身份验证。 此示例中的配置文件显式将mode>的属性设置为,并将Message属性设置为clientCredentialType。 这些值是此绑定的默认值,但它们已显式配置,如以下示例配置中所示,用于演示它们的使用。
<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 示例 中的说明执行操作。