WCF ClearUsernameBinding:无需 SSL 或 x.509 证书发送用户名






4.53/5 (9投票s)
如何使用 WCF 发送明文用户名,无需 SSL 或 X.509 证书

引言
用户名/密码对是 Web 服务中一种常见的身份验证机制。但是,WCF 限制了用户名仅能在启用 SSL 或 x.509 的场景中使用。ClearUsernameBinding 缓解了这种限制。
背景
Web 服务中最常见的身份验证机制之一是消息级别的用户名/密码。 看起来像这样
<wsse:Username>yaron</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/
oasis-200401-wss-username-token-profile-1.0#PasswordText">1234</wsse:Password>
由于密码以明文形式出现,因此任何看到此消息的人都可以稍后入侵系统。
因此,应该使用传输级别的 SSL 或消息级别的 X.509 证书。
实际上,当我们想要使用用户名时,WCF 强制我们使用这些机制之一。
否则,我们将收到以下任何异常(请参阅完整列表)
The provided URI scheme 'http' is invalid; expected 'https'.
Parameter name: via
The provided URI scheme 'https' is invalid; expected 'http'.
Parameter name: via
BasicHttp binding requires that BasicHttpBinding.Security.Message.ClientCredentialType
be equivalent to the BasicHttpMessageCredentialType.Certificate
credential type for secure messages.
Select Transport or TransportWithMessageCredential security for UserName credentials.
Could not find a base address that matches scheme https for the endpoint
with binding BasicHttpBinding.
Registered base address schemes are [http].
虽然这是一个很好的做法,但这种建议对互操作性产生了负面影响,因为其他框架创建的 Web 服务可能需要明文用户名。 在某些情况下,我们也确实想要这样做,例如在内部安全网络中或在使用负载均衡器 SSL 直通(例如 F5 的 BIG-IP)时。
运行演示项目
ClearUsernmaeBinding
代码带有一个演示项目。 为了运行演示
- 将本文附加的代码提取到某个文件夹,例如C:\program files\ClearUsernameBinding\
- 运行服务器
C:\program files\ClearUsernameBinding\TestService\bin\Release\TestService.exe
- 运行客户端
C:\program files\ClearUsernameBinding\TestClient\bin\Release\TestClient.exe
您将拥有一个使用明文用户名的 WCF 工作演示!
Using the Code
该代码是一个新的 WCF 绑定,您可以在项目中使用。
此博客文章包含有关如何使用它的其他信息。
请遵循以下步骤
- 将代码提取到某个文件夹(例如“ClearUsernameBinding”)
- 在您的 WCF 项目中,添加对 ClearUsernameBinding\ClearUserPassBinding\bin\Release\ClearUsernameBinding.dll 的引用(根文件夹是您提取到的文件夹)
- 在 web.config 或 app.config 中,在
system.ServiceModel
部分注册并配置绑定
<system.serviceModel>
<client>
<endpoint address=https://.:8087/SampleService/
binding="clearUsernameBinding"
bindingConfiguration="myClearUsernameBinding"
contract="ServiceReference1.IEchoService"
name="ClearUsernameBinding_IEchoService" />
</client>
<extensions>
<bindingExtensions>
<add name="clearUsernameBinding"
type="WebServices20.BindingExtenions.ClearUsernameCollectionElement,
ClearUsernameBinding" />
</bindingExtensions>
</extensions>
<bindings>
<clearUsernameBinding>
<binding name="myClearUsernameBinding" messageVersion="Soap12">
</binding>
</clearUsernameBinding>
</bindings>
</system.serviceModel>
- 在同一个配置文件中,配置您的终结点以使用
ClearUsernameBinding
<endpoint binding="clearUsernameBinding"
bindingConfiguration="myClearUsernameBinding"
contract="WebServices20.SampleService.IEchoService" />
更多信息
- ClearUsernameBinding 在 Google 代码中
- 对 ClearUsernameBinding 的支持
- 有关 WCF 中的用户名的更多详细信息
- 使用用户名/密码保护 Web 服务
- WCF 博客
绑定作者
Yaron Naveh 是一位 Web 服务互操作性专家。
他的博客包含有关各种框架(WCF、WSE、WSIT、Axis2...)的互操作性的信息,并且涉及 Web 服务安全性、性能和测试。
历史
- 2009 年 9 月 6 日:首次发布