如何在web.config中为CustomBinding配置TransportSecurityBindingElement?

时间:2012-09-11 15:29:40

标签: c# .net wcf

我有以下(简化)代码,我想通过web.config配置

var security = new TransportSecurityBindingElement();
security.EndpointSupportingTokenParameters.SignedEncrypted.Add(new UserNameSecurityTokenParameters());

var binding = new CustomBinding(security);

是否可以使用Web配置配置此自定义绑定?如果可能,我如何配置支持令牌参数的端点?我已尝试过以下配置,但它不会对DotNetConfig.xsd进行验证:

<system.serviceModel>
  <bindings>
    <customBinding>
      <binding name="SomeBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00">
        <transportSecurity> <!-- Fails validation -->
            <!-- How do I configure the EndpointSupportingTokenParameters -->             
        </transportSecurity>
      </binding>
    </customBinding>
  </bindings>
  <client>
    ...
  </client>
</system.serviceModel>

1 个答案:

答案 0 :(得分:0)

我认为您只使用customBinding/security,可能使用authenticationMode UserNameOverTransport

<system.serviceModel>
    <bindings>
      <customBinding>
        <binding>
          <security authenticationMode="UserNameOverTransport" />
        </binding>
      </customBinding>
    </bindings>
</system.serviceModel>

如果失败,您可以尝试手动将令牌添加到issuedTokenParameters

<system.serviceModel>
    <bindings>
      <customBinding>
        <binding>
          <security authenticationMode="UserNameOverTransport">
              <issuedTokenParameters tokenType="http://schemas.microsoft.com/ws/2006/05/identitymodel/tokens/UserName" />
          </security>
        </binding>
      </customBinding>
    </bindings>
</system.serviceModel>

(来自UserNameSecurityTokenParameters的tokenType被盗)

说实话,这可能是非常麻烦,以至于在代码中执行它会更容易。如果每个环境需要不同,请在配置中定义工厂并使用DI来创建绑定。