如何在WCF配置中为自定义绑定添加UsernameToken

时间:2016-07-25 10:46:45

标签: c# .net wcf ws-security

我有一个WCF客户端,其工作自定义绑定满足UsernameToken-Policy等。现在我想将此绑定外包给.config。这是我在代码中的绑定:

AsymmetricSecurityBindingElement asymmetricBinding =
    SecurityBindingElement.CreateMutualCertificateDuplexBindingElement(
        MessageSecurityVersion.WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10);
asymmetricBinding.InitiatorTokenParameters = new X509SecurityTokenParameters { InclusionMode = SecurityTokenInclusionMode.AlwaysToRecipient };
asymmetricBinding.RecipientTokenParameters = new X509SecurityTokenParameters { InclusionMode = SecurityTokenInclusionMode.Never };
asymmetricBinding.EndpointSupportingTokenParameters.Signed.Add(new UserNameSecurityTokenParameters
{
    InclusionMode = SecurityTokenInclusionMode.AlwaysToRecipient
});
asymmetricBinding.DefaultAlgorithmSuite = SecurityAlgorithmSuite.Basic256;
asymmetricBinding.IncludeTimestamp = true;
asymmetricBinding.SecurityHeaderLayout = SecurityHeaderLayout.Strict;
asymmetricBinding.MessageProtectionOrder = MessageProtectionOrder.SignBeforeEncrypt;
//...

this.Elements.Add(asymmetricBinding);
this.Elements.Add(new TextMessageEncodingBindingElement(MessageVersion.Soap11, Encoding.UTF8));
this.Elements.Add(new HttpsTransportBindingElement
{
    AuthenticationScheme = AuthenticationSchemes.Basic,
    KeepAliveEnabled = false,
    AllowCookies = false
});

我在上面的代码中添加了UserNameSecurityTokenParameters来添加令牌(它使用ClientCredentials.UserName.UserName和ClientCredentials.UserName.Password)。但在WCF配置中我还没有找到任何添加此令牌的选项。到目前为止,我的.config看起来像这样:



<customBinding>
        <binding name="WsSecurityBinding">
          <security defaultAlgorithmSuite="Default" enableUnsecuredResponse="true"
            authenticationMode="MutualCertificateDuplex" requireDerivedKeys="false"
            securityHeaderLayout="Strict" includeTimestamp="true" allowInsecureTransport="true"
            messageProtectionOrder="SignBeforeEncrypt" messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10"
            requireSignatureConfirmation="false"/>
          <context protectionLevel="EncryptAndSign" />
          <textMessageEncoding messageVersion="Soap11" writeEncoding="utf-8" />
          <httpsTransport maxReceivedMessageSize="1000000" allowCookies="false"
            authenticationScheme="Basic" keepAliveEnabled="false" requireClientCertificate="false" />
        </binding>
      </customBinding>
&#13;
&#13;
&#13;

有没有人知道如何在WCF .config中设置此UsernameToken?

0 个答案:

没有答案