public static void Main(string[] args) { HttpListener listener = new HttpListener(); //Used to keep the listener alive for multiple requests. //The listener keeps listening for incoming requests as long as the value is true. bool listen = true; try { listener.Prefixes.Add(prefixIPAddress); listener.Start(); } catch(Exception ex) { return; } while (listen) { HttpListenerContext context = listener.GetContext(); if (!ValidateRequests(context)) { continue; } Task.Run(delegate () { try { if (listen) { HttpListenerRequest request = context.Request; //Ignore requests from favicon.ico in case of browser requests. if (!(request.Url.LocalPath.Contains("favicon.ico"))) { HttpListenerResponse response = context.Response; if (string.IsNullOrEmpty(request.Url.LocalPath) || string.Equals("/", request.Url.LocalPath)) { PingMethod(response, request); } else { PutFile(CoreHelper.HttpsServerConfiguration.ConfigPath, request, response); } } else { return; } } } catch (Exception ex) { listen = false; } }); } listener.Stop(); } private static bool ValidateRequests(HttpListenerContext context) { string errorMessage = string.Empty; if(!CoreHelper.HttpsServerConfiguration.EnableMutualAuthentication) { return true; } // Check if client certificate is present X509Certificate2 clientCertificate = GenericHelper.GetClientCertificate(context); if(clientCertificate == null) { return false; } if (!GenericHelper.ValidateClientCertificate(clientCertificate, out errorMessage)) { return false; } return true; } public static X509Certificate2 GetClientCertificate(HttpListenerContext context) { // Retrieve the client certificate from the request X509Certificate clientCert = context.Request.GetClientCertificate(); return clientCert as X509Certificate2; } internal static bool ValidateClientCertificate(X509Certificate2 clientCertificate, out string errorMessage) { //Currently returning true, need to add validation for the received client certificate. return true; }