WS2007FederationHttp 示例演示了如何使用WS2007FederationHttpBinding标准绑定来生成支持 WS-Trust 规范版本 1.3 的联合方案。
注释
本示例的设置过程和生成说明位于本主题末尾。
此示例由基于控制台的客户端程序(Client.exe)、基于控制台的安全令牌服务程序(Securitytokenservice.exe)和基于控制台的服务计划(Service.exe)组成。 该服务实现定义请求/回复通信模式的协定。 协定由ICalculator接口定义,该接口公开数学运算(Add、Subtract和MultiplyDivide)。 客户端从安全令牌服务(STS)获取安全令牌,并为给定的数学运算向服务发出同步请求。 然后,服务会回复结果。 客户端活动在控制台窗口中可见。
该示例通过ICalculator元素使ws2007FederationHttpBinding协定可用。 客户端上此绑定的配置显示在以下代码中:
<bindings>
<ws2007FederationHttpBinding>
<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" >
<!-- Endpoint address and binding for Security Token Service -->
<issuer address ="http://localhost:8000/sts/windows"
binding ="ws2007HttpBinding" />
</message>
</security>
</binding>
</ws2007FederationHttpBinding>
</bindings>
在 <security> 上,security 值配置应使用的安全模式。 在此示例中,message使用安全性,因此消息<被指定在<内>。
<message> 内的 <issuer> 元素针对向客户端颁发安全令牌的 STS 指定地址和绑定,以便客户端能够向 ICalculator 服务进行身份验证。
此绑定在服务上的配置显示在以下代码中:
<bindings>
<ws2007FederationHttpBinding>
<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" >
<!-- Metadata address for Security Token Service -->
<issuerMetadata address ="http://localhost:8000/sts/mex" >
<identity>
<certificateReference storeLocation ="CurrentUser"
storeName="TrustedPeople"
x509FindType ="FindBySubjectDistinguishedName"
findValue ="CN=STS" />
</identity>
</issuerMetadata>
</message>
</security>
</binding>
</ws2007FederationHttpBinding>
</bindings>
在 <security> 上,security 值配置应使用的安全模式。 在此示例中,message使用安全性,因此消息<被指定在<内>。
<消息>中的>元素指定了可用于检索STS元数据的终结点的地址和标识。
服务的行为显示在以下代码中:
<behaviors>
<serviceBehaviors>
<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>
</serviceBehaviors>
</behaviors>
<issuedTokenAuthentication>> 使服务可以对它在身份验证过程中允许客户端出示的令牌指定约束。 此配置指定服务将接受由主题名称为 CN=STS 的证书签名的令牌。
STS 使用标准 WS2007HttpBinding提供单个终结点。 该服务响应来自客户端的令牌请求。 如果使用 Windows 帐户对客户端进行身份验证,该服务会颁发一个令牌,其中包含客户端的用户名作为声明。 在创建令牌过程中,STS 使用与 CN=STS 证书关联的私钥对令牌进行签名。 此外,它还创建对称密钥,并使用与 CN=localhost 证书关联的公钥对其进行加密。 在将令牌返回到客户端时,STS 还会返回对称密钥。 客户端向服务提供颁发的令牌 ICalculator ,并通过使用该密钥对消息进行签名来证明它知道对称密钥。
运行示例时,安全令牌请求会显示在 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.bat 文件允许使用相关证书配置服务器和 STS,以运行自承载应用程序。 批处理文件在 LocalMachine/TrustedPeople 证书存储中创建两个证书。 第一个证书的使用者名称为 CN=STS,由 STS 用来对颁发给客户端的安全令牌进行签名。 第二个证书的主题名称为 CN=localhost,由 STS 用来加密密钥,以便服务能够解密。
设置、生成和运行示例
确保已为 Windows Communication Foundation 示例 执行One-Time 安装过程。
使用管理员权限打开 Visual Studio 的开发人员命令提示符,并运行 Setup.bat 文件来创建所需的证书。
此批处理文件使用与 Windows SDK 一起分发的Certmgr.exe 和 Makecert.exe。 但是,必须从 Visual Studio 命令提示符内运行 Setup.bat ,使脚本能够查找这些工具。
若要生成解决方案的 C# 或 Visual Basic .NET 版本,请按照 生成 Windows Communication Foundation 示例中的说明进行操作。
要使用单机配置或跨计算机配置运行示例,请按照运行 Windows Communication Foundation 示例中的说明进行操作。 如果使用 Windows Vista,则必须使用提升的权限运行 Service.exe、 Client.exe和 SecurityTokenService.exe (右键单击文件,然后单击以 管理员身份运行)。