如何为单个方案配置多个WCF绑定配置

San*_*zen 5 wcf .net-4.0 wcf-binding wcf-security

我有一组IIS7托管的net.tcp WCF服务,它们为我的ASP.NET MVC Web应用程序提供服务.Web应用程序可通过Internet访问.

WCF Services (IIS7) <--> ASP.NET MVC Application <--> Client Browser

这些服务是用户名验证的,客户端(我的Web应用程序)用于登录的帐户最终作为主机上的当前主体.

我希望其中一个服务的身份验证方式不同,因为它为我的登录视图提供了视图模型.当它被调用时,客户端显然还没有登录.我认为,如果服务托管在与Web应用程序不在同一域中的计算机上,则Windows身份验证可以提供最佳或可能只是基于证书的安全性(实际上我也应该用于经过身份验证的服务).

但这不是重点.使用多个TCP绑定是给我带来麻烦的.我在我的客户端配置中尝试将其设置为:

<bindings>
  <netTcpBinding>
    <binding>
      <security mode="TransportWithMessageCredential">
        <message clientCredentialType="UserName"/>
      </security>
    </binding>
    <binding name="public">
      <security mode="Transport">
        <message clientCredentialType="Windows"/>
      </security>
    </binding>
  </netTcpBinding>
</bindings>

<client>
  <endpoint contract="Server.IService1" binding="netTcpBinding" address="net.tcp://localhost:8081/Service1.svc"/>
  <endpoint contract="Server.IService2" binding="netTcpBinding" bindingConfiguration="public" address="net.tcp://localhost:8081/Service2.svc"/>
</client>
Run Code Online (Sandbox Code Playgroud)

服务器配置如下:

<bindings>
  <netTcpBinding>
    <binding portSharingEnabled="true">
      <security mode="TransportWithMessageCredential">
        <message clientCredentialType="UserName"/>
      </security>
    </binding>
    <binding name="public">
      <security mode="Transport">
        <message clientCredentialType="Windows"/>
      </security>
    </binding>
  </netTcpBinding>
</bindings>

<services>
  <service name="Service1">
    <endpoint contract="Server.IService1, Library" binding="netTcpBinding" address=""/>
  </service>
  <service name="Service2">
    <endpoint contract="Server.IService2, Library" binding="netTcpBinding" bindingConfiguration="public" address=""/>
  </service>
</services>

<serviceHostingEnvironment>
  <serviceActivations>
    <add relativeAddress="Service1.svc" service="Server.Service1"/>
    <add relativeAddress="Service2.svc" service="Server.Service2"/>
  </serviceActivations>
</serviceHostingEnvironment>
Run Code Online (Sandbox Code Playgroud)

问题是两个绑定似乎都不想在我的主机中共存.当我删除它们中的任何一个时,一切都很好,但它们一起在客户端上产生以下异常:

"net.tcp:// localhost:8081/Service2.svc"不支持请求的升级.这可能是由于绑定不匹配(例如在客户端而不是在服务器上启用安全性).

在服务器跟踪日志中,我发现以下异常:

协议类型应用程序/协商已发送到不支持该类型升级的服务.

我正在寻找正确的方向还是有更好的方法来解决这个问题?

UPDATE

虽然这个问题似乎相当陈旧,但它仍然与我相关(我也想到其他人).目前,我正在使用一个神奇的用户名/密码组合(因为当前主体需要一个用户名)访问不应该首先进行身份验证的服务.根据这个问题,您可以看到我宁愿为这些公共服务专门设置未经身份验证的绑定.在这种情况下,一个神奇的帐户不是不安全的,它不提供除公共级别以外的任何访问.

小智 4

尝试使服务能够使用多个绑定:

<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
Run Code Online (Sandbox Code Playgroud)