WCF imperative binding

2.8k views Asked by At

How to translate the following declarative (via configuration file) binding, to imperative binding (hardcoded inside application)?

<system.serviceModel>
        <bindings>
            <customBinding>
                <binding name="CustomBinding_IAEService">
                    <security defaultAlgorithmSuite="Basic256Sha256Rsa15" authenticationMode="MutualCertificateDuplex"
                        requireDerivedKeys="false" securityHeaderLayout="Lax" includeTimestamp="true" allowSerializedSigningTokenOnReply="true"
                        keyEntropyMode="CombinedEntropy" messageProtectionOrder="SignBeforeEncrypt"
                        messageSecurityVersion="Default" requireSignatureConfirmation="false">
                        <localClientSettings cacheCookies="true" detectReplays="true"
                            replayCacheSize="900000" maxClockSkew="00:05:00"
                            replayWindow="00:05:00" sessionKeyRenewalInterval="10:00:00"
                            sessionKeyRolloverInterval="00:05:00" reconnectTransportOnFailure="true"
                            timestampValidityDuration="00:05:00" cookieRenewalThresholdPercentage="60" />
                        <localServiceSettings detectReplays="true" issuedCookieLifetime="10:00:00"
                            maxStatefulNegotiations="128" replayCacheSize="900000" maxClockSkew="00:05:00"
                            negotiationTimeout="00:01:00" replayWindow="00:05:00" inactivityTimeout="00:02:00"
                            sessionKeyRenewalInterval="15:00:00" sessionKeyRolloverInterval="00:05:00"
                            reconnectTransportOnFailure="true" maxPendingSessions="128"
                            maxCachedCookies="1000" timestampValidityDuration="00:05:00" />
                        <secureConversationBootstrap />
                    </security>
                    <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
                        messageVersion="Soap12" writeEncoding="utf-8">
                        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    </textMessageEncoding>
                    <httpsTransport manualAddressing="false" maxBufferPoolSize="524288"
                        maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous"
                        bypassProxyOnLocal="false" decompressionEnabled="true" hostNameComparisonMode="StrongWildcard"
                        keepAliveEnabled="true" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous"
                        realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"
                        useDefaultWebProxy="true" requireClientCertificate="false" />
                </binding>
            </customBinding>
        </bindings>

Thanks

EDIT:

After Adislav's replay I tryed with:

    // configure security properties
    AsymmetricSecurityBindingElement security = SecurityBindingElement.CreateMutualCertificateDuplexBindingElement(MessageSecurityVersion.Default);
    security.DefaultAlgorithmSuite = SecurityAlgorithmSuite.Basic256Sha256Rsa15;
    security.SecurityHeaderLayout = SecurityHeaderLayout.Lax;
    security.AllowSerializedSigningTokenOnReply = true;
    security.KeyEntropyMode = SecurityKeyEntropyMode.CombinedEntropy;
    security.MessageProtectionOrder = MessageProtectionOrder.SignBeforeEncrypt;
    security.RequireSignatureConfirmation = false;

    // configure encoding properties
    TextMessageEncodingBindingElement encoding = new TextMessageEncodingBindingElement();

    // configure transport properties
    HttpsTransportBindingElement transport = new HttpsTransportBindingElement();

    CustomBinding customBinding = new CustomBinding(security, encoding, transport);

But this code doesn't work. What do I miss?

Thanks

1

There are 1 answers

1
Ladislav Mrnka On BEST ANSWER

You have to use System.ServiceModel.Channels.CustomBinding class and related binding elements (from the same namespace):

  var security = SecurityBindingElement.CreateMutualCertificateDuplexBindingElement(MessageSecurityVersion.Default);
  // configure security properties
  var encoding = new TextMessageEncodingBindingElement();
  // configure encoding properties
  var transport = new HttpsTransportBindingElement();
  // configure transport properties
  var customBinding = new CustomBinding(security, encoding, transport);