Nick__Dudas
Nick__Dudas

Reputation: 886

HttpListener server returns an error: (503) Service Unavailable

I am trying to create a http listener that uses x509 certificates for client/server authentication.

My server code is as follows.

_listener = new HttpListener();
_listener.Prefixes.Add("https://localhost:8006/");
_listener.Start();
HttpListenerContext context = _listener.GetContext();

My client code is as follows

string url = "https://localhost:8006/";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
var store = new X509Store(StoreName.TrustedPeople, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
var cert = store.Certificates.Find(X509FindType.FindBySubjectName, "localhost", true);
request.ClientCertificates.Add((X509Certificate)cert[0]);
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, policyErrs) =>
   {
       return policyErrs == System.Net.Security.SslPolicyErrors.None;
   };
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

I believe I have configured everything correctly. There are no certificate policy errors, I have bound the ssl cert to the port, and require no elevated permissions to run the listener.

enter image description here

If I make a web request in code or through Chrome I get this error. What am I doing wrong here?

enter image description here

Upvotes: 3

Views: 4486

Answers (1)

Nick__Dudas
Nick__Dudas

Reputation: 886

Turns out the prefix was incorrect, once I changed 'localhost' to '+' everything worked fine.

Upvotes: 7

Related Questions