English 中文(简体)
Binding an SSL certificate to a port programmatically
原标题:

I m working on a self-hosted WCF service for which encrypted communications is an option. Everything works fine when a certificate is already bound to the port as described here.

However, I want to avoid asking the user to run a command line tool. Is there a way the binding can be done programmatically? Perhaps using WMI?

最佳回答

I believe the way to create an HTTP.SYS namespace reservation is through the HttpSetServiceConfiguration() unmanaged API; so you ll need some P/Invoke for that. There s some sample code that might be useful in one of Keith Brown s MSDN columns.

问题回答

I found this NuGet library that can help to programmatically bind an SSL certificate.

The question it s rather old, but I think this library could be helpful, as it was with me, for those who seek a pure .Net programmatic way to bind a SSL certificate to a IP endpoint.

Library name (NuGet): SslCertBinding.Net

Sources: GitHub NuGet package sources

Library description:

SslCertBinding.Net is a library for .NET and Windows and provides a simple API to add, remove or retrieve bindings between a https port and a SSL certificate.

Credits: Segor

This library is built upon Win32API calls.

For the usage, have a look to the GitHub NuGet package sources link above, Usage section.

The MSDN documentation, Keith Brown s MSDN column, and pinvoke.net got me most of the way there. But getting the PSOCKADDR in the HTTP_SERVICE_CONFIG_SSL_KEY correct was tricky. I found Beej s Guide to Network Programming very helpful in figuring out what it should look like. I was able to use the .NET SocketAddress and then copy the bytes to an array that could be marshaled.

// serialize the endpoint to a SocketAddress and create an array to hold the values.  Pin the array.
SocketAddress socketAddress = ipEndPoint.Serialize();
byte[] socketBytes = new byte[socketAddress.Size];
GCHandle handleSocketAddress = GCHandle.Alloc(socketBytes, GCHandleType.Pinned);

// Should copy the first 16 bytes (the SocketAddress has a 32 byte buffer, the size will only be 16, which is what the SOCKADDR accepts
for (int i = 0; i < socketAddress.Size; ++i)
{
    socketBytes[i] = socketAddress[i];
}

Yes, but you have to use the HTTP API yourself which which there is currently no .NET wrapper, so you must use P/Invoke. Specifically I think you re looking for HttpSetServiceConfiguration with the HttpServiceConfigSSLCertInfo config id.





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签