WS 联合身份验证 HTTP 绑定

本示例演示如何使用 Windows Communication Foundation (WCF) 实现典型的联合身份验证服务、安全令牌服务和相应的客户端。此示例由客户端控制台程序 (client.exe)、安全令牌服务控制台程序 (Securitytokenservice.exe) 和服务控制台程序 (Service.exe) 组成。该服务实现定义“请求-答复”通信模式的协定。该协定由 ICalculator 接口定义,此接口公开数学运算(加、减、乘和除)。客户端从安全令牌服务 (STS) 中获取安全令牌,向给定数学运算的服务发出同步请求,服务使用结果进行回复。客户端活动显示在控制台窗口中。

有关 此绑定的更多信息,请参见How to: Create a WSFederationHttpBindingHow 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 指定地址和绑定,以使客户端能够向计算器服务进行身份验证。

下面是服务上此绑定的配置。

<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值配置应该使用的安全模式。在本示例中使用了消息安全性,这也是在 security element of wsFederationHttpBinding内指定 <message> 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> 使服务可以指定对令牌的约束,客户端可以在身份验证过程中出示该令牌。此配置指定该服务接受由主题名称为 CN=STS 的证书签名的令牌。

STS 使用标准 wsHttpBinding 公开单个终结点。STS 响应客户端对令牌的请求、使用 Windows 帐户提供客户端身份验证并颁发包含客户端用户名(作为颁发的令牌中的声明)的令牌。作为创建令牌的一部分,STS 使用与 CN=STS 证书关联的私钥对令牌进行签名。另外,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 用此证书加密机密,以便服务能够解密该机密。

设置、生成和运行示例

  1. 运行 Setup.cmd 文件以创建所需的证书。

  2. 若要生成 C# 或 Visual Basic .NET 版本的解决方案,请按照生成 Windows Communication Foundation 示例中的说明进行操作。确保生成解决方案中的所有项目(Client、RSTRSTR、SecurityTokenService、Service 和 Shared)。

  3. 确保 Service.exe 和 SecurityTokenService.exe 都在运行。

  4. 运行 Client.exe。

另请参见

任务

WS 2007 联合 HTTP 绑定

Send comments about this topic to Microsoft.
© 2007 Microsoft Corporation. All rights reserved.