WCF - How to Create a Binary Encrypted Binding over HTTP (S)

I would like to convert my current WCF HTTP / HTTPS binding settings to use binary message encoding, and I need to do this in code, not in XML configuration. AFAIK, I need to create a CustomBinding object and set the appropriate BindingElements, but I cannot figure out which elements should be used in my script.

Main parameters of my WCF configuration:

  • use HTTP or HTTPS transport depending on the configuration (in app.config)
  • use username message security
  • todo: add binary encoding instead of default text

My current code for setting the binding (works, but without binary encoding):

var isHttps = Settings.Default.wcfServiceBaseAddress.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase);
var binding = new WSHttpBinding(isHttps ? SecurityMode.TransportWithMessageCredential : SecurityMode.Message);
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;

I tried to use this code, but it doesn’t work - I don’t know how to set the message security element for username message security:

var custBinding = new CustomBinding();
custBinding.Elements.Add(new BinaryMessageEncodingBindingElement());
//Transport Security (Not Required)
if (isHttps)
{
    custBinding.Elements.Add(SecurityBindingElement.CreateUserNameForSslBindingElement());
}
//Transport (Required)
custBinding.Elements.Add(isHttps ?
    new HttpsTransportBindingElement() :
    new HttpTransportBindingElement());

Does anyone know how to set this up? I tried to find a similar problem / solution, but failed ...

+5
source share
3 answers

I almost forgot this question, but here is my custom binding class that works with binary binding over HTTP by checking username and password, and also allows you to enable GZip compression ...

    public class CustomHttpBinding: CustomBinding
{
    private readonly bool useHttps;
    private readonly bool useBinaryEncoding;
    private readonly bool useCompression;
    private readonly HttpTransportBindingElement transport;

    public CustomHttpBinding(bool useHttps, bool binaryEncoding = true, bool compressMessages = false)
    {
        this.useHttps = useHttps;
        transport = useHttps ? new HttpsTransportBindingElement() : new HttpTransportBindingElement();
        useBinaryEncoding = binaryEncoding;
        useCompression = compressMessages;
    }

    public long MaxMessageSize{set
    {
        transport.MaxReceivedMessageSize = value;
        transport.MaxBufferSize = (int) value;
    }}

    public override BindingElementCollection CreateBindingElements()
    {
        BindingElement security;
        if (useHttps)
        {
            security = SecurityBindingElement.CreateSecureConversationBindingElement(
                SecurityBindingElement.CreateUserNameOverTransportBindingElement());
        }
        else
        {
            security = SecurityBindingElement.CreateSecureConversationBindingElement(
                SecurityBindingElement.CreateUserNameForSslBindingElement(true));
        }

        MessageEncodingBindingElement encoding;
        if (useCompression)
        {
            encoding = new GZipMessageEncodingBindingElement(useBinaryEncoding
                                                                ? (MessageEncodingBindingElement)
                                                                  new BinaryMessageEncodingBindingElement()
                                                                : new TextMessageEncodingBindingElement());
        }
        else
        {
            encoding = useBinaryEncoding
                        ? (MessageEncodingBindingElement) new BinaryMessageEncodingBindingElement()
                        : new TextMessageEncodingBindingElement();
        }

        return new BindingElementCollection(new[]
            {
                security,
                encoding,
                transport,
            });
    }
}
+7
source

Try SecurityBindingElement.CreateUserNameOverTransportBindingElement()instead:

var custBinding = new CustomBinding();
custBinding.Elements.Add(new BinaryMessageEncodingBindingElement());
//Transport Security (Not Required)
if (isHttps)
{
  custBinding.Elements.Add(SecurityBindingElement.CreateUserNameOverTransportBindingElement());
}
//Transport (Required)
custBinding.Elements.Add(isHttps ?
   new HttpsTransportBindingElement() :
   new HttpTransportBindingElement());
0
source

SecurityBindingElement AllowInsecureTransport. true, HttpTransportBindingElement .

0
source

All Articles