将WCF与负载平衡(AWS)一起使用时无效的安全上下文令牌

Zan*_*non 3 c# wcf load-balancing amazon-web-services

我有一个托管在AWS上的WCF应用程序.为了实现更高的可用性,我创建了WCF计算机的快照,并使用此映像启动了另一个实例.

此外,我创建了一个Elastic Load Balance(ELB),用于将请求路由到这两个服务器.

使用我的WCF客户端,如果我使用机器公共IP地址,我可以成功连接两台服务器.但是,如果我使用ELB主机名,我的连接将失败,并显示以下错误:

System.ServiceModel.FaultException:无法处理消息.这很可能是因为" http://tempuri.org/IService/GetCustomerData "操作不正确,或者因为邮件包含无效或过期的安全上下文令牌,或者因为绑定之间存在不匹配.如果服务由于不活动而中止了通道,则安全上下文令牌将无效.要防止服务中止空闲会话,请过早增加服务端点绑定的接收超时.

该错误表明我的安全令牌无效或过期.所以,我检查了发送和接收超时,它已经设置为10分钟:sendTimeout="00:10:00",receiveTimeout="00:10:00" (请求通常需要5-15秒)

我的招标配置:

<bindings>
  <wsHttpBinding>
    <binding name="wsHttpBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" useDefaultWebProxy="true">
      <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
      <security mode="TransportWithMessageCredential">
        <transport clientCredentialType="Basic"/>
        <message clientCredentialType="UserName"/>
      </security>
    </binding>
  </wsHttpBinding>
</bindings>
Run Code Online (Sandbox Code Playgroud)

另外,我经过双重检查:

  • ELB和机器防火墙在端口80和443上打开.
  • 我的ELB配置了正确的证书,并在端口443上有一个监听器.
  • 我在两台机器上都有一个IIS Web服务器.如果我使用ELB地址,一切正常.
  • 如果ELB路由到一台机器,WCF可以工作.如果到两台机器的路由,WCF失败.

Zan*_*non 10

我设法解决了这个问题,添加了以下参数:establishSecurityContext="false".

<bindings>
  <wsHttpBinding>
    <binding name="wsHttpBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" useDefaultWebProxy="true">
      <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
      <security mode="TransportWithMessageCredential">
        <transport clientCredentialType="Basic"/>
        <message clientCredentialType="UserName" 
                 establishSecurityContext="false"/> <!-- this line -->
      </security>
    </binding>
  </wsHttpBinding>
</bindings>
Run Code Online (Sandbox Code Playgroud)

谷歌搜索,我了解到:

当此值设置为false时,必须使用非对称加密进行密钥交换和验证.此参数的默认值为true,这意味着第一次调用将创建具有非对称加密的安全上下文,但它将被缓存,而其他调用将仅使用对称加密,这会更快.

表现考虑:Yaron Naveh

当期望客户端连续进行多次调用时,最好将此参数设置为true,但是如果使用负载平衡,则会将呼叫路由到不同的服务器,这会因无效令牌而中断消息.因此,您必须禁用此安全上下文功能.

建立安全上下文部分的详细说明:https://msdn.microsoft.com/en-us/library/hh273122(v = vs.100).aspx