WCF与传输和消息安全性的安全通信

时间:2012-07-16 12:44:00

标签: wcf security certificate

我正在尝试了解WCF及其所有(复杂)安全选项。我在localhost上开发了一个带有wsHttpBinding的Wcf webservice。我正在创建一个控制台应用程序,我将分发给几个客户端。每个客户端都有自己的用户名和密码,我将根据自己的数据库进行验证。为此,我在codebetter.com上遵循了以下教程:http://codebetter.com/petervanooijen/2010/03/22/a-simple-wcf-service-with-username-password-authentication-the-things-they-don-t-tell-you/

  • 我获得了自签名证书,并授予了权限
  • 我得到了服务器&客户端在localhost上完全设置并运行到localhost。

问题1:

我使用VS2010添加服务对话框添加了svc服务,并将以下标记添加到我的app.config中:

<certificate encodedValue="AwA<...>" />

出于学习目的,我改变了这个值几次,但它没有任何效果。它保持完美的工作。我使用Fiddler查看加密通信。

问题2:使用SSL在测试服务器上部署

我已经在配置了IIS7.5和SSL的测试服务器上部署了托管WCF服务的Web应用程序。当我尝试将客户端应用程序连接到测试服务器时,它抛出了一般错误。我发现当我从https://更改为http://时,它工作正常。 Fiddler用加密的值显示了很好的http通信。

但我也想使用HTTPS和我的消息级加密。我认为这是不可能的。我把它全部工作了,只需将安全模式设置为“TransportWithMessageCredential”就完成了。但是,当我这样做时,我可以删除客户端上的所有证书信息,它仍然有效。因此,我可以得出结论,传输安全性(种类)否决了Message安全性。

我正在做出正确的结论:

  • 没有https传输安全性?使用证书(服务器上的pfx,客户端上的证书)
  • 获得https传输安全性?不要使用证书,SSL就足够了。

我希望自己明白:)。非常感谢提前。

以下几个(匿名)配置:

服务器

<bindings>
  <wsHttpBinding>
    <!--The maximum size, in bytes, for a message that is processed by the binding-->
    <binding name="Binding1" maxReceivedMessageSize="4194304">
      <readerQuotas maxDepth="32" maxStringContentLength="10485760" maxArrayLength="10485760"
        maxBytesPerRead="10485760" maxNameTableCharCount="10485760" />
      <security mode="TransportWithMessageCredential">
        <message clientCredentialType="UserName" negotiateServiceCredential="false" />
      </security>
    </binding>
  </wsHttpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior name="ApiWcfCustomBehavior">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceCredentials>
        <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="CustomUserNameValidator, SupplierAPI"/>
        <serviceCertificate findValue="xxxxApiv3" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName"/>
      </serviceCredentials>
    </behavior>
  </serviceBehaviors>
</behaviors>

*客户*

var endPoint = new EndpointAddress(new Uri(apiUrl), EndpointIdentity.CreateDnsIdentity(endpointDnsEntity));

var binding = new WSHttpBinding();
//binding.Security.Mode = SecurityMode.TransportWithMessageCredential;
binding.Security.Mode = SecurityMode.Message;
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
binding.Security.Message.NegotiateServiceCredential = false;

using (var client = new SupplierServiceClient(binding, endPoint))
{
    client.ClientCredentials.UserName.UserName = supplierUID;
    client.ClientCredentials.UserName.Password = supplierPassword;

    client.ClientCredentials.ServiceCertificate.SetDefaultCertificate(
         System.Security.Cryptography.X509Certificates.StoreLocation.LocalMachine,
         System.Security.Cryptography.X509Certificates.StoreName.My,
         System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectName,
         endpointDnsEntity);
 }

1 个答案:

答案 0 :(得分:0)

我正在尝试做类似的事情(SSL,消息加密,用户名/密码认证),我也开始认为这是不可能的,至少不是没有我不知道的一些黑客行为。如果我正确理解this post,当您的客户端计算机通过SSL直接连接到您的服务计算机时,您将拥有点对点连接,这意味着它通过SSL保护,并且身份验证(用户名/密码值)是安全的使用WS-Security。

该帖子中使用的有趣类比是基于SSL的在线银行业务。由于客户端和服务器具有点对点连接,因此您将拥有安全通道。

这正是我发现的,所以如果有人认为这是不正确的,请告诉我。