简体   繁体   中英

Enable SSL binding for self-hosted WCF service

I run a self-hosted WCF service which I want to protect with self-signed SSL certificate. For that to work I need to bind this SSL certificate with certain port. There are couple ways of doing it:

  1. According to this http://msdn.microsoft.com/en-us/library/ms733791.aspx I can use netsh and it works but I would like not to have dependencies on the netsh and do everything from the .NET code.

  2. So I found the following snippet:

      using (ServerManager manager = new ServerManager()) { Site site = manager.Sites.FirstOrDefault(i => i.Applications.Cast<Application>().Any()); Application app = site.Applications.Cast<Application>().First(); site.Bindings.Add(":*:PORTN", cert.GetCertHash(), store.Name); manager.CommitChanges(); } 

It works, however this required IIS to be installed. Otherwise I'm getting the following exception:

Retrieving the COM class factory for component with CLSID {2B72133B-3F5B-4602-8952-803546CE3344} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))..

So, question is - is there any way to accomplish this inside managed code, but without having to install IIS?

Found the correct answer to my question: Binding an SSL certificate to a port programmatically

Instead of using managed classes like ServerManager I would need to rely on P/Invoking native API such as HttpSetServiceConfiguration .

is there any way to accomplish this inside managed code, but without having to install IIS?

Yes, and given that you are running as a service with enough privileges to call NETSH to do the allow and disallow as needed using Process.Start and WaitForExit and can achieve what you need in your question.

The command line syntax http://technet.microsoft.com/en-us/library/cc725882(WS.10).aspx for NETSH allows you to do everything you need.

If you want to figure out all the P/Invoke but it sounds like a lot of extra trouble, but that sounds like academic exercise with more risk of memory leaks and so on.

If you are running on Windows XP and are worried about NETSH not doing what you need, then you can also get Microsoft's HTTPCFG here http://msdn.microsoft.com/en-us/library/windows/desktop/aa364478%28v=vs.85%29.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM