Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring SSL on ASP.NET Self-Hosted Web API

I'm creating Self-hosted Web API service. To secure it, I've studied and implemented this article, successfully generated an local SSL Certificate using makecert and my service authenticated and generates tokens fine, if I'm using

http://localhost/webapi/authentication/authenticate 

link, but when I try to access my service using HTTPS, I get following on Firefox:

ssl_error_rx_record_too_long

and for the same request Fiddler shows me:

HTTP/1.1 502 Fiddler - Connection Failed Date: Mon, 26 Aug 2013 10:44:27 GMT Content-Type: text/html; charset=UTF-8 Connection: close Timestamp: 13:44:27.433

[Fiddler] The socket connection to localhost failed.
Failed to negotiate HTTPS connection with server.fiddler.network.https> Failed to secure existing connection for localhost. The handshake failed due to an unexpected packet format..

My self-host configuration:

    private HttpSelfHostServer _server;     private ExtendedHttpsSelfHostConfiguration _config;     public const string ServiceAddress = "https://localhost/webapi";     _config = new ExtendedHttpsSelfHostConfiguration(ServiceAddress);     _server = new HttpSelfHostServer(_config);     _server.OpenAsync(); 

where ExtendedHttpSelfHostConfiguration taken from this post is:

public class ExtendedHttpSelfHostConfiguration : HttpSelfHostConfiguration {     public ExtendedHttpSelfHostConfiguration(string baseAddress) : base(baseAddress) { }     public ExtendedHttpSelfHostConfiguration(Uri baseAddress) : base(baseAddress) { }      protected override BindingParameterCollection OnConfigureBinding(HttpBinding httpBinding)     {         if (BaseAddress.ToString().ToLower().Contains("https://"))         {             httpBinding.Security.Mode = HttpBindingSecurityMode.Transport;         }          return base.OnConfigureBinding(httpBinding);     } } 

What I'm missing? Thanks in advance!

like image 551
insomnium_ Avatar asked Aug 26 '13 11:08

insomnium_


1 Answers

According to this blog post I've figured out, that I should create an SSL certificate and assign it to specific port (:99 in my case).

I've created locally signed SSL. Then got it's Thumbprint and ApplicationId. Using CMD command netsh (in pre Win7 systems there is an httpcfg tool), I've assigned my certificate to the port

netsh http add sslcert ipport=0.0.0.0:99 certhash=3e49906c01a774c888231e5092077d3d855a6861 appid={2d6059b2-cccb-4a83-ae08-8ce209c2c5c1}, where certhash = SSL Thumbprint, and appid = ApplicationId I've copied earlier.

That's it, now I'm able to make HTTPS requests!

like image 163
insomnium_ Avatar answered Oct 04 '22 15:10

insomnium_