title: Creating and using custom self signed certificates without installing
body: <p>This is what I want to achieve:</p>
  <ul>
  <li>Communicate securely with the ASP.NET server</li>
  <li>Without installing certificates on the client or using third party CAs</li>
  </ul>
  <p>I intend to bundle the public certificate with the client application and host the client application on something like Firebase (supports HTTPS), so the certificate can't be manipulated.</p>
  <p>For this I generated certificate files using these commands:</p>
  <pre class="lang-sh prettyprint-override"><code># private.pem | Creates RSA key
  openssl genrsa -out private.pem 8192
  # certificate.csr | Creates certificate request
  openssl req -new -key private.pem -out certificate.csr
  # certificate.crt | Signs certificate request. File for client
  openssl x509 -req -days 3000 -in certificate.csr -signkey private.pem -out certificate.crt
  # private.pfx | Creates file for server
  openssl pkcs12 -export -out private.pfx -inkey private.pem -in certificate.crt
  </code></pre>
  <p>That is how I used the private.pfx file in the server:</p>
  <pre class="lang-cs prettyprint-override"><code>// Startup.cs -&gt; class Startup
  public static IHostBuilder CreateHostBuilder(string[] args)
  {
      return Host.CreateDefaultBuilder(args)
          .ConfigureWebHostDefaults(webBuilder =&gt;
          {
              webBuilder.UseStartup&lt;Startup&gt;();
              // This is what i added
              webBuilder.UseKestrel(options =&gt;
              {
                  options.ConfigureHttpsDefaults(
                      adapterOptions =&gt;
                      {
                          adapterOptions.ClientCertificateMode = ClientCertificateMode.RequireCertificate;
                          // Should use certificate with private key
                          adapterOptions.ServerCertificate = new X509Certificate2(&quot;./private.pfx&quot;, &quot;&quot;);
                      });
              });
          });
  }
  </code></pre>
  <p>The server launches without any errors</p>
  <p>On the client side I tested it using C# | .Net 5</p>
  <pre class="lang-cs prettyprint-override"><code>// I created a new HttpClientHandler
  var handler = new HttpClientHandler();
              handler.ClientCertificateOptions = ClientCertificateOption.Manual;
  // I need to add this in order to specify the certificate
  handler.ClientCertificateOptions = ClientCertificateOption.Manual;
  // I load and specify the public certificate
  handler.ClientCertificates.Add(new X509Certificate2(&quot;certificate.crt&quot;));
  // The handler is then used In an HTTP client
  var hcl = new HttpClient(handler);
  </code></pre>
  <p>However when calling the server I get the following exception:</p>
  <pre class="lang-cs prettyprint-override"><code>Grpc.Core.RpcException: Status(StatusCode=&quot;Internal&quot;, Detail=&quot;Error starting gRPC call.
   HttpRequestException: The SSL connection could not be established, see inner exception. AuthenticationException: The remote certificate is invalid according to the validation procedure: RemoteCertificateNameMismatch, RemoteCertificateChainErrors&quot;, DebugException=&quot;System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
   ---&gt; System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure: RemoteCertificateNameMismatch, RemoteCertificateChainErrors
  </code></pre>
  <p>What am I doing wrong, and how do I fix it?</p>