65.9K
CodeProject 正在变化。 阅读更多。
Home

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

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.53/5 (9投票s)

2009年9月6日

CPOL

2分钟阅读

viewsIcon

72614

downloadIcon

1487

如何使用 WCF 发送明文用户名,无需 SSL 或 X.509 证书

coolcode

引言

用户名/密码对是 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 代码带有一个演示项目。 为了运行演示

  1. 将本文附加的代码提取到某个文件夹,例如C:\program files\ClearUsernameBinding\
  2. 运行服务器
    C:\program files\ClearUsernameBinding\TestService\bin\Release\TestService.exe
  3. 运行客户端
    C:\program files\ClearUsernameBinding\TestClient\bin\Release\TestClient.exe

您将拥有一个使用明文用户名的 WCF 工作演示!

WCF ClearUsernameBinding

Using the Code

该代码是一个新的 WCF 绑定,您可以在项目中使用。
博客文章包含有关如何使用它的其他信息。

请遵循以下步骤

  1. 将代码提取到某个文件夹(例如“ClearUsernameBinding”)
  2. 在您的 WCF 项目中,添加对 ClearUsernameBinding\ClearUserPassBinding\bin\Release\ClearUsernameBinding.dll 的引用(根文件夹是您提取到的文件夹)
  3. web.configapp.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>
  1. 在同一个配置文件中,配置您的终结点以使用 ClearUsernameBinding
<endpoint binding="clearUsernameBinding" 
	bindingConfiguration="myClearUsernameBinding"
                    contract="WebServices20.SampleService.IEchoService" />

更多信息

绑定作者

Yaron Naveh 是一位 Web 服务互操作性专家。
他的博客包含有关各种框架(WCF、WSE、WSIT、Axis2...)的互操作性的信息,并且涉及 Web 服务安全性、性能和测试

历史

  • 2009 年 9 月 6 日:首次发布
© . All rights reserved.