Code Example
按照下列过程可以配置客户端凭据并将其传递给使用各种形式的 Windows 身份验证(客户端凭据除外)的 Web 服务。 对于该例外情况,请按照“客户端证书身份验证”一节中的过程操作。
将 Web 服务配置为使用 Windows 身份验证
- 使用 IIS 将 Web 服务配置为使用 Windows 身份验证。 - IIS 允许指定目录或文件级别的安全性。 如果要按文件指定 Web 服务的安全性,请在 IIS 中的 .asmx 文件中为 Web 服务设置相应的权限。 .asmx 文件是 Web 服务的入口点。 有关详细信息,请参见 IIS 文档。 
- 修改配置文件以指定 Windows 身份验证。 - 在配置文件中,将 authentication XML 元素的 mode 属性设置为“Windows”。 下面的代码示例将配置文件修改为使用 Windows 身份验证。 - // Fragment of a Web.config file. <authentication mode= "Windows"> </authentication>
将客户端凭据传递给使用 Windows 身份验证的 Web 服务
- 为 Web 服务的代理类创建一个新实例。 如果尚未生成代理类,请参见创建 XML Web services 代理以了解详细信息。 
- 创建 NetworkCredential 类的一个新实例,并设置 UserName、Password 和 Domain 属性。 
- 创建 CredentialCache 的一个新实例。 
- 使用 CredentialCache 的 Add 方法将 NetworkCredential 添加到 CredentialCache 中。 
- 将 CredentialCache 的该实例分配给代理类的 Credentials 属性。 - 如果使用集成 Windows 身份验证,则必须将 Credentials 属性设置为 System.Net.CredentialCache.DefaultCredentials。 - 如果将 Credentials 属性设置为 DefaultCredentials,则客户端将与服务器进行协商,并根据服务器的配置方式执行 Kerberos 和/或 NTLM 身份验证。 
- 下面的代码示例设置向使用 Windows 身份验证的 Web 服务方法传递的客户端凭据。 
客户端证书身份验证
按照下列过程可以配置客户端凭据并将其传递给使用客户端凭据形式的 Windows 身份验证的 Web 服务。
将 Web 服务配置为使用客户端证书身份验证
- 下面的列表概述了如何将 IIS 配置为使用客户端证书对客户端进行身份验证。 有关详细信息,请参见 IIS 文档。 
- 安装 SSL。 
- 将 Web 应用程序配置为接受客户端证书。 
- 修改配置文件,以指定对 Web 服务进行 Windows 身份验证。 - 在配置文件中,将 authentication XML 元素的 mode 属性设置为“Windows”。 下面的代码示例将配置文件修改为使用 Windows 身份验证。 - // Fragment of a Web.config file. <authentication mode= "Windows"> </authentication>
将客户端凭据传递给使用客户端证书身份验证的 Web 服务
- 为 Web 服务的代理类创建一个新实例。 如果尚未生成代理类,请参见创建 XML Web services 代理以了解详细信息。 
- 创建 X509Certificate 的一个新实例。 
- 调用 CreateFromCertFile 方法,以便从文件中加载客户端证书。 - 客户端可以从受信任的证书颁发机构那里获取客户端证书文件。 有关详细信息,请参见 IIS 文档。 
- 将 X509Certificate 添加到代理类的 ClientCertificates ClientCertificates 集合中。 - 下面的代码示例演示 Web 服务客户端如何使用客户端证书传递其凭据。 从 Web 服务器颁发的客户端证书将由 CreateFromCertFile 方法从文件中加载并添加到代理类的 ClientCertificates 属性中。 - ' Instantiate proxy class to a Bank Web service. Dim bank As BankSession = new BankSession() ' Load the client certificate from a file. Dim x509 As X509Certificate = X509Certificate.CreateFromCertFile("c:\user.cer") ' Add the client certificate to the ClientCertificates property ' of the proxy class. bank.ClientCertificates.Add(x509) ' Call the method on the proxy class, which requires authentication ' using client certificates. bank.Deposit(500)- // Instantiate proxy class to a Bank Web service. BankSession bank = new BankSession(); // Load the client certificate from a file. X509Certificate x509 = X509Certificate.CreateFromCertFile(@"c:\user.cer"); // Add the client certificate to the ClientCertificates property // of the proxy class. bank.ClientCertificates.Add(x509); // Call the method on the proxy class, which requires // authentication using client certificates. bank.Deposit(500);
示例
如果将 Credentials 属性设置为 System.Net.CredentialCache.DefaultCredentials,则客户端将与服务器进行协商,并根据服务器的配置方式执行 Kerberos 和/或 NTLM 身份验证。
下面的代码示例设置向使用 Windows 身份验证的 Web 服务方法传递的客户端凭据。
Imports System
Imports System.Web.Services.Protocols
Imports System.Net
Imports MyMath
Public Class Calculator
   Public Shared Sub Main()
     ' Create a new instance of the proxy class to an
     ' Web service method. 
     Dim mathproxy As MyMath.Math = New MyMath.Math()
     
     ' Create a new instance of CredentialCache.
     Dim mycredentialCache As CredentialCache = New CredentialCache()
     ' Create a new instance of NetworkCredential using the client
     ' credentials.
       Dim credentials As NetworkCredential = New _          NetworkCredential(UserName,SecurelyStoredPasword,Domain)
     ' Add the NetworkCredential to the CredentialCache.
       mycredentialCache.Add(New Uri(mathproxy.Url), "Basic", _                             credentials)
     ' Add the CredentialCache to the proxy class credentials.
     mathproxy.Credentials = mycredentialCache
     ' Call the method on the proxy class.
     Dim result As Integer 
     result = mathproxy.Add(3,5)
  End Sub
End Class 
using System;
using System.Web.Services.Protocols;
using System.Net;
using MyMath;
public class Calculator
{
  public static void Main() 
  {
     // Create a new instance of the proxy class to an XML
     // Web service method. 
     MyMath.Math math = new MyMath.Math();
    // Create a new instance of CredentialCache.
    CredentialCache credentialCache = new CredentialCache();
   // Create a new instance of NetworkCredential using the client
   // credentials.
   NetworkCredential credentials = new
      NetworkCredential(UserName,SecurelyStroredPassword,Domain);
   // Add the NetworkCredential to the CredentialCache.
   credentialCache.Add(new Uri(math.Url),                        "Basic", credentials);
   // Add the CredentialCache to the proxy class credentials.
   math.Credentials = credentialCache;
     // Call the method on the proxy class.
     int result = math.Add(3,5);
  }
}
请参见
任务
参考
NetworkCredential
CredentialCache
X509Certificate
概念
保证使用 ASP.NET 创建的 XML Web services 的安全
其他资源
ASP.NET Web Application Security
使用 ASP.NET 的 XML Web services
.gif)
版权所有 (C) 2007 Microsoft Corporation。保留所有权利。