WS 聯合 HTTP 繫結
這個範例會示範如何使用 Windows Communication Foundation (WCF) 實作典型的聯合服務、安全性權杖服務和對應的用戶端。這個範例是由用戶端主控台程式 (client.exe)、安全性權杖服務主控台程式 (Securitytokenservice.exe) 和服務主控台程式 (Service.exe) 所組成。服務會實作定義要求-回覆通訊模式的合約。合約是由 ICalculator
介面所定義,這個介面會公開數學運算作業 (加、減、乘、除)。用戶端會從安全性權杖服務 (STS) 取得安全性權杖,並對指定的數學運算作業提出同步要求,服務則會以結果回覆。您可以在主控台視窗中看到用戶端活動。
如需 這個繫結的詳細資訊,請參閱 How to: Create a WSFederationHttpBinding 和 How to: Configure Credentials on a Federation Service。
注意: |
---|
此範例的安裝程序與建置指示位於本主題的結尾。 |
這個範例會使用 wsFederationHttpBinding Element公開 ICalculator
合約。這個繫結的下列組態位於用戶端上。
<bindings>
<wsFederationHttpBinding>
<binding name="ServiceFed" >
<security mode ="Message">
<message issuedKeyType ="SymmetricKey"
issuedTokenType ="http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1" >
<issuer address ="https://localhost:8000/sts/windows"
binding ="wsHttpBinding" />
</message>
</security>
</binding>
</wsFederationHttpBinding>
</bindings>
在 security element of wsFederationHttpBinding上,security mode 值會設定應該使用的安全性模式。這個範例中所使用的是訊息安全性,因此會在 security element of wsFederationHttpBinding中指定 message element of wsFederationHttpBinding。message element of wsFederationHttpBinding內 wsFederationHttpBinding 的 <issuer> 項目會指定將安全性權杖發行給用戶端之 STS 的位址與繫結,以便用戶端可以驗證至 Calculator 服務。
這個繫結的下列組態位於服務上。
<bindings>
<wsFederationHttpBinding>
<binding name="ServiceFed" >
<security mode ="Message">
<message issuedKeyType ="SymmetricKey"
issuedTokenType =
"http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1" >
<issuerMetadata address ="https://localhost:8000/sts/mex" >
<identity>
<certificateReference storeLocation ="CurrentUser"
storeName="TrustedPeople"
x509FindType ="FindBySubjectNameDistinguisedName"
findValue ="CN=STS"/>
</identity>
</issuerMetadata>
</message>
</security>
</binding>
</wsFederationHttpBinding>
</bindings>
在 security element of wsFederationHttpBinding上,security mode 值會設定應該使用的安全性模式。本範例中會使用訊息安全性,因此 <message> element of wsFederationHttpBinding是指定在 security element of wsFederationHttpBinding中。<message> element of wsFederationHttpBinding內 wsFederationHttpBinding 的 <issuerMetadata> 項目會指定端點的位址與身分識別,這個端點可用於擷取 STS 的中繼資料。
服務的行為如下列程式碼中所示。
<behavior name ="ServiceBehaviour" >
<serviceDebug includeExceptionDetailInFaults ="true"/>
<serviceMetadata httpGetEnabled ="true"/>
<serviceCredentials>
<issuedTokenAuthentication>
<knownCertificates>
<add storeLocation ="LocalMachine"
storeName="TrustedPeople"
x509FindType="FindBySubjectDistinguishedName"
findValue="CN=STS" />
</knownCertificates>
</issuedTokenAuthentication>
<serviceCertificate storeLocation ="LocalMachine"
storeName ="My"
x509FindType ="FindBySubjectDistinguishedName"
findValue ="CN=localhost"/>
</serviceCredentials>
</behavior>
<issuedTokenAuthentication> of <serviceCredentials> 允許服務在其允許於驗證期間提出之用戶端的權杖上指定條件約束 (Constraint)。這個組態會指定服務接受主體名稱為 CN=STS 之憑證所簽署的權杖。
STS 會使用標準 wsHttpBinding 公開單一端點。STS 會做出回應以向用戶端要求權杖,然後再發行包含用戶端使用者名稱 (當做發行權杖中的宣告) 的權杖 (假設用戶端驗證是使用 Windows 帳戶)。做為建立權杖的一部分,STS 會使用與 CN=STS 憑證相關聯的私密金鑰簽署權杖。此外,它還會建立對稱金鑰,並使用與 CN=localhost 憑證相關聯的公開金鑰加密。將權杖傳回用戶端時,STS 也會傳回對稱金鑰。用戶端會向計算機服務出示發行權杖,然後使用對稱金鑰簽署訊息來證明它知道這把金鑰。
執行範例
請參閱下列指示以執行範例。當您執行範例時,安全性權杖的要求會顯示在 STS 主控台視窗中。作業要求和回應會顯示在用戶端和服務主控台視窗中。在任何主控台視窗中按下 ENTER 鍵,即可關閉應用程式。
Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714
Press <ENTER> to terminate client.
設定批次檔
本範例中所包含的 Setup.cmd 批次檔可讓您使用相關的憑證設定伺服器和安全性權杖服務 (STS),以執行自我裝載應用程式。批次檔會在 LocalMachine/TrustedPeople 憑證存放區中建立兩個憑證。第一個憑證的主體名稱為 CN=STS,會由 STS 用來簽署核發給用戶端的安全性權杖。第二個憑證的主體名稱為 CN=localhost,STS 可用於加密服務可以解密的密碼。
若要設定、建置及執行範例
執行 Setup.cmd 檔以建立所需的憑證。
若要建置方案的 C# 或 Visual Basic .NET 版本,請遵循建置 Windows Communication Foundation 範例中的指示。請確保已建置方案中的所有專案 (用戶端、RSTRSTR、SecurityTokenService、服務、共用)。
確定 Service.exe 和 SecurityTokenService.exe 都在執行中。
執行 Client.exe。
請參閱
工作
Send comments about this topic to Microsoft.
© 2007 Microsoft Corporation. All rights reserved.