Share via


HOW TO:設定 XML Web Service 進行 Windows 驗證

本主題專門說明舊有技術。 應該使用下列建立 XML Web Service 及 XML Web Service 用戶端: Windows Communication Foundation.

Code Example

請依照下列程序,設定用戶端認證並將它傳遞給使用除了「用戶端認證」以外所有形式之 Windows 驗證的 Web 服務。對於這個情況,請依照「用戶端憑證驗證」一節中的程序執行。

設定 Web 服務進行 Windows 驗證

  1. 使用 IIS 將 Web 服務設定為使用 Windows 驗證。

    IIS 允許您指定目錄或檔案層級的安全性。如果您想要針對個別檔案指定 Web 服務的安全性,請在 IIS 的 .asmx 檔案中設定 Web 服務的權限。.asmx 檔案是 Web 服務的進入點。如需詳細資料,請參閱 IIS 文件。

  2. 修改組態檔以指定 Windows 驗證。

    在組態檔中,將 authentication XML 項目的 mode 屬性設定為 "Windows"。下列程式碼範例會修改組態檔以使用 Windows 驗證。

    // Fragment of a Web.config file.
    <authentication mode= "Windows">
    </authentication> 
    

使用 Windows 驗證將用戶端認證傳遞給 Web 服務

  1. 在 Web 服務中建立 Proxy 類別的新執行個體。如果沒有產生 Proxy 類別,請參閱建立 XML Web Service Proxy 以了解詳細資料。

  2. 建立 NetworkCredential 類別的新執行個體,並設定 UserNamePasswordDomain 屬性。

  3. 建立 CredentialCache 的新執行個體。

  4. 使用 CredentialCacheAdd 方法,將 NetworkCredential 新增至 CredentialCache

  5. CredentialCache 的執行個體指派給 Proxy 類別的 Credentials 屬性。

    如果是使用整合式 Windows 驗證,您就必須將 Credentials 屬性設定為 System.Net.CredentialCache.DefaultCredentials

    Credentials 屬性設定為 DefaultCredentials 時,用戶端會與伺服器進行交涉,根據伺服器的設定方式來決定執行 Kerberos 和/或 NTLM 驗證。

  6. 下列程式碼範例會設定傳遞給使用 Windows 驗證之 Web 服務方法的用戶端認證。

用戶端憑證驗證

請依照下列程序,設定用戶端認證,並將它傳遞給使用「用戶端認證」形式之 Windows 驗證的 Web 服務。

設定 Web 服務進行用戶端憑證驗證

  1. 下列清單是有關如何設定 IIS 以驗證使用用戶端憑證之用戶端的概觀。如需詳細資料,請參閱 IIS 文件。

  2. 安裝 SSL。

  3. 設定 Web 應用程式接受用戶端憑證。

  4. 修改組態檔來為 Web 服務指定 Windows 驗證。

    在組態檔中,將 authentication XML 項目的 mode 屬性設定為 "Windows"。下列程式碼範例會修改組態檔以使用 Windows 驗證。

    // Fragment of a Web.config file.
    <authentication mode= "Windows">
    </authentication>
    

使用用戶端憑證驗證將用戶端認證傳遞給 Web 服務

  1. 在 Web 服務中建立 Proxy 類別的新執行個體。如果沒有產生 Proxy 類別,請參閱建立 XML Web Service Proxy 以了解詳細資料。

  2. 建立 X509Certificate 的新執行個體。

  3. 叫用 CreateFromCertFile 方法,從檔案中載入用戶端憑證。

    用戶端可以從信任的憑證授權單位取得用戶端憑證檔案。如需詳細資料,請參閱 IIS 文件。

  4. X509Certificate 新增至 Proxy 類別的 ClientCertificates ClientCertificates 集合。

    下列程式碼範例示範 Web 服務用戶端如何使用用戶端憑證傳遞其認證。從 Web 伺服器核發的用戶端憑證會透過 CreateFromCertFile 方法從檔案載入,然後新增至 Proxy 類別的 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);
  }
}

另請參閱

工作

HOW TO:使用 SOAP 標頭執行自訂驗證

參考

NetworkCredential
CredentialCache
X509Certificate

概念

為使用 ASP.NET 建立的 XML Web Service 設定安全性

其他資源

ASP.NET Web Application Security
使用 ASP.NET 的 XML Web Service