共用方式為


HOW TO:指定通道安全性認證

Windows Communication Foundation (WCF) 服務 Moniker 允許 COM 應用程式呼叫 WCF 服務。大部分 WCF 服務都會要求用戶端必須指定驗證和授權的認證。從 WCF 用戶端呼叫 WCF 服務時,您可以在 Managed 程式碼或應用程式組態檔中指定這些認證。如果是從 COM 應用程式呼叫 WCF 服務,則可以使用 IChannelCredentials 介面來指定認證。本主題將說明各種使用 IChannelCredentials 介面指定認證的方式。

ms735113.note(zh-tw,VS.100).gif注意:
IChannelCredentials 是以 IDispatch 為基礎的介面,因此您無法在 Visual Studio 環境中使用 IntelliSense 功能。

本文將會使用訊息安全性範例中定義的 WCF 服務。

若要指定用戶端憑證

  1. 執行 Message Security 目錄中的 Setup.bat 檔案以建立並安裝必要的測試憑證。

  2. 開啟「訊息安全性」專案。

  3. [ServiceBehavior(Namespace=``http://Microsoft.ServiceModel.Samples``)] 加入至 ICalculator 介面定義。

  4. bindingNamespace=``http://Microsoft.ServiceModel.Samples 加入至服務的 App.config 中的端點標記 (Tag)。

  5. 建置「訊息安全性範例」並執行 Service.exe。使用 Internet Explorer 並瀏覽至服務的 URI (https://localhost:8000/ServiceModelSamples/Service),以確定該服務正在運作。

  6. 開啟 Visual Basic 6.0,並建立新的標準 .exe 檔案。將按鈕新增至表單中,然後按兩下這個按鈕,將下列程式碼新增至 Click 處理常式:

        monString = "service:mexAddress=https://localhost:8000/ServiceModelSamples/Service?wsdl"
        monString = monString + ", address=https://localhost:8000/ServiceModelSamples/Service"
        monString = monString + ", contract=ICalculator, contractNamespace=http://Microsoft.ServiceModel.Samples"
        monString = monString + ", binding=BasicHttpBinding_ICalculator, bindingNamespace=http://Microsoft.ServiceModel.Samples"
    
        Set monikerProxy = GetObject(monString)
    
        'Set the Service Certificate.
     monikerProxy.ChannelCredentials.SetServiceCertificateAuthentication "CurrentUser", "NoCheck", "PeerOrChainTrust"
    monikerProxy.ChannelCredentials.SetDefaultServiceCertificateFromStore "CurrentUser", "TrustedPeople", "FindBySubjectName", "localhost"
    
        'Set the Client Certificate.
        monikerProxy.ChannelCredentials.SetClientCertificateFromStoreByName "CN=client.com", "CurrentUser", "My"
        MsgBox monikerProxy.Add(3, 4)
    
  7. 執行 Visual Basic 應用程式及驗證結果。

    Visual Basic 應用程式將會顯示含有呼叫 Add(3, 4) 之結果的訊息方塊。您也可以使用 SetClientCertificateFromFileSetClientCertificateFromStoreByName 替代 SetClientCertificateFromStore 來設定用戶端憑證:

    monikerProxy.ChannelCredentials.SetClientCertificateFromFile "C:\MyClientCert.pfx", "password", "DefaultKeySet"
    
ms735113.note(zh-tw,VS.100).gif注意:
用戶端憑證必須在執行用戶端的電腦上受信任,這個呼叫才會有作用。

ms735113.note(zh-tw,VS.100).gif注意:
如果 Moniker 的格式錯誤或服務無法使用,則呼叫 GetObject 時將會傳回「無效的語法」錯誤。如果您收到這個錯誤,請確定您所使用的 Moniker 正確無誤,而且此服務為可用狀態。

若要指定使用者名稱和密碼

  1. 將服務的 App.config 檔案修改為使用 wsHttpBinding。這對使用者名稱和密碼驗證而言是必要的動作:

    <endpoint address=""
              binding="wsHttpBinding"
              bindingNamespace="http://Microsoft.ServiceModel.Samples" 
              bindingConfiguration="Binding1" 
              contract="Microsoft.ServiceModel.Samples.ICalculator" />
    
  2. clientCredentialType 設定為使用者名稱:

    <bindings>
      <wsHttpBinding>
        <binding name="Binding1" >
          <security mode="Message">
             <message clientCredentialType="UserName"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    
  3. 開啟 Visual Basic 6.0,並建立新的標準 .exe 檔案。將按鈕新增至表單中,然後按兩下這個按鈕,將下列程式碼新增至 Click 處理常式:

        monString = "service:mexAddress=https://localhost:8000/ServiceModelSamples/Service?wsdl"
        monString = monString + ", address=https://localhost:8000/ServiceModelSamples/Service"
        monString = monString + ", contract=ICalculator, contractNamespace=http://Microsoft.ServiceModel.Samples"
        monString = monString + ", binding=WSHttpBinding_ICalculator, bindingNamespace=http://Microsoft.ServiceModel.Samples"
    
        Set monikerProxy = GetObject(monString)
    
        monikerProxy.ChannelCredentials.SetServiceCertificateAuthentication "CurrentUser", "NoCheck", "PeerOrChainTrust"
        monikerProxy.ChannelCredentials.SetUserNameCredential "username", "password"
    
        MsgBox monikerProxy.Add(3, 4)
    
  4. 執行 Visual Basic 應用程式及驗證結果。Visual Basic 應用程式將會顯示含有呼叫 Add(3, 4) 之結果的訊息方塊。

    ms735113.note(zh-tw,VS.100).gif注意:
    在這個範例的服務 Moniker 中指定的繫結已經變更為 WSHttpBinding_ICalculator。另請注意,您必須在對 SetUserNameCredential 的呼叫中提供有效的使用者名稱和密碼。

若要指定 Windows 認證

  1. 在服務的 App.config 檔案中,將 clientCredentialType 設定為 Windows:

    <bindings>
      <wsHttpBinding>
        <binding name="Binding1" >
          <security mode="Message">
            <message clientCredentialType="Windows"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    
  2. 開啟 Visual Basic 6.0,並建立新的標準 .exe 檔案。將按鈕加入至表單中,然後按兩下這個按鈕,將下列程式碼加入至 Click 處理常式:

        monString = "service:mexAddress=https://localhost:8000/ServiceModelSamples/Service?wsdl"
        monString = monString + ", address=https://localhost:8000/ServiceModelSamples/Service"
        monString = monString + ", contract=ICalculator, contractNamespace=http://Microsoft.ServiceModel.Samples"
        monString = monString + ", binding=WSHttpBinding_ICalculator, bindingNamespace=http://Microsoft.ServiceModel.Samples"
        monString = monString + ", upnidentity=domain\userID"
    
        Set monikerProxy = GetObject(monString)
         monikerProxy.ChannelCredentials.SetWindowsCredential "domain", "userID", "password", 1, True
    
        MsgBox monikerProxy.Add(3, 4)
    
  3. 執行 Visual Basic 應用程式及驗證結果。Visual Basic 應用程式將會顯示含有呼叫 Add(3, 4) 之結果的訊息方塊。

    ms735113.note(zh-tw,VS.100).gif注意:
    您必須以有效值取代 "domain"、"userID" 和 "password"。

指定發行權杖

  1. 發行權杖僅適用於使用聯合安全性的應用程式。如需聯合安全性的詳細資訊,請參閱聯合與發行的權杖聯合範例

    下列 Visual Basic 程式碼範例示範如何呼叫 SetIssuedToken 方法:

        monString = "service:mexAddress=https://localhost:8000/ServiceModelSamples/Service?wsdl"
        monString = monString + ", address=https://localhost:8000/SomeService/Service"
        monString = monString + ", contract=ICalculator, contractNamespace=http://SomeService.Samples"
        monString = monString + ", binding=WSHttpBinding_ISomeContract, bindingNamespace=http://SomeService.Samples"
    
        Set monikerProxy = GetObject(monString)
    monikerProxy.SetIssuedToken("http://somemachine/sts", "bindingType", "binding")
    

    如需此方法之參數的詳細資訊,請參閱 SetIssuedToken

另請參閱

工作

HOW TO:設定聯合服務的認證
HOW TO:建立聯合用戶端

概念

聯合
WCF 中的訊息安全性
繫結和安全性