本主题演示如何启用 Windows Communication Foundation (WCF) 服务进行身份验证的客户端使用 Windows 域用户名和密码。 它假定您有一个正在工作的自承载 WCF 服务。 有关创建基本自承载的 WCF 服务,请参阅示例入门教程。 本主题假定在代码中配置服务。 如果你想要看到的配置类似服务使用配置文件示例请参阅用户名消息安全
要配置服务以使用 Windows 域用户名和密码对其客户端进行身份验证,请使用 xref:System.ServiceModel.WSHttpBinding,并将其 Security.Mode 属性设置为 Message。 此外,您必须指定 X509 证书,该证书用于在将用户名和密码从客户端发送到服务时对它们进行加密。
在客户端上,您必须提示用户输入用户名和密码,并指定用户在 WCF 客户端代理上的凭据。
若要配置使用 Windows 域用户名和密码进行身份验证的 WCF 服务
-
创建 xref:System.ServiceModel.WSHttpBinding 的实例,将绑定的安全模式设置为 xref:System.ServiceModel.WSHttpSecurity.Message?displayProperty=nameWithType,将绑定的
ClientCredentialType设置为 xref:System.ServiceModel.MessageCredentialType.UserName?displayProperty=nameWithType,并使用配置的绑定将服务终结点添加到服务主机,如下面的代码中所示:// ... WSHttpBinding userNameBinding = new WSHttpBinding(); userNameBinding.Security.Mode = SecurityMode.Message; userNameBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName; svcHost.AddServiceEndpoint(typeof(IService1), userNameBinding, ""); // ... -
指定用于对通过网络发送的用户名和密码信息进行加密的服务器证书。 此代码应紧跟在上面的代码之后。 下面的示例使用的 setup.bat 文件创建的证书用户名消息安全示例:
// ... svcHost.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName, "localhost"); // ...您可以使用您自己的证书,只需修改代码以引用您的证书。 有关创建和使用证书的详细信息请参阅Working with Certificates。 确保证书在本地计算机的受信任人证书存储区中。 您可以执行此操作通过运行 mmc.exe 并选择文件,添加/删除管理单元中... 菜单项。 在中添加或删除管理单元对话框中,选择管理单元中的证书然后单击添加。 在证书管理单元对话框中选择计算机帐户。 默认情况下,从消息安全用户名称示例生成的证书将位于个人/证书文件夹中。 它将列为"localhost"颁发给在 MMC 窗口中的列。 拖放到到证书受信任的人员文件夹。 这将允许 WCF 在执行身份验证时,将证书视为受信任的证书。
调用服务传递用户名和密码
-
客户端应用程序必须提示用户输入其用户名和密码。 下面的代码要求用户输入用户名和密码。
WARNING 此代码不应在生产中使用,因为密码在输入时将显示出来。
public static void GetPassword(out string username, out string password) { Console.WriteLine("Provide a valid machine or domain account. [domain\\user]"); Console.WriteLine(" Enter username:"); username = Console.ReadLine(); Console.WriteLine(" Enter password:"); password = Console.ReadLine(); return; } -
创建客户端代理的一个实例,用于指定客户端的证书,如下面的代码所示:
string username; string password; // Instantiate the proxy Service1Client proxy = new Service1Client(); // Prompt the user for username & password GetPassword(out username, out password); // Set the user’s credentials on the proxy proxy.ClientCredentials.UserName.UserName = username; proxy.ClientCredentials.UserName.Password = password; // Treat the test certificate as trusted proxy.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.PeerOrChainTrust; // Call the service operation using the proxy
请参阅
- xref:System.ServiceModel.WSHttpBinding
- xref:System.ServiceModel.WSHttpSecurity
- xref:System.ServiceModel.SecurityMode
- xref:System.ServiceModel.Security.UserNamePasswordClientCredential.UserName*
- xref:System.ServiceModel.Security.UserNamePasswordClientCredential.Password*
- xref:System.ServiceModel.Security.UserNamePasswordClientCredential
- xref:System.ServiceModel.WSHttpSecurity.Mode*
- xref:System.ServiceModel.HttpTransportSecurity.ClientCredentialType*
- 使用基本身份验证的传输安全性
- 分布式应用程序安全
- <wsHttpBinding>
历史资料。此文对应 2019-08-18 的中文文档快照,适用产品版本和命令行为以原文为准。
来源与署名:Microsoft 和文档贡献者。固定版本原文。文档采用 CC BY 4.0,示例代码采用 MIT 授权。本站将 Markdown 转为 HTML,规范化链接,移除外部媒体、发布器占位标签和导航标记。
