<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by bN on Medium]]></title>
        <description><![CDATA[Stories by bN on Medium]]></description>
        <link>https://medium.com/@bnobo?source=rss-c8704d7888e0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*IUThQmelAvxq7mOt.jpg</url>
            <title>Stories by bN on Medium</title>
            <link>https://medium.com/@bnobo?source=rss-c8704d7888e0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 00:46:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bnobo/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Free certified SSL certificate in ASP.NET 5 Kestrel application]]></title>
            <link>https://bnobo.medium.com/free-certified-ssl-certificate-in-asp-net-5-kestrel-application-7051c3f3902c?source=rss-c8704d7888e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/7051c3f3902c</guid>
            <category><![CDATA[net5]]></category>
            <category><![CDATA[kestrel]]></category>
            <category><![CDATA[tls]]></category>
            <category><![CDATA[ssl]]></category>
            <category><![CDATA[lets-encrypt]]></category>
            <dc:creator><![CDATA[bN]]></dc:creator>
            <pubDate>Mon, 12 Apr 2021 17:05:51 GMT</pubDate>
            <atom:updated>2021-04-12T17:05:51.293Z</atom:updated>
            <content:encoded><![CDATA[<p>It’s possible to obtain a free SSL certificate thanks to <a href="https://letsencrypt.org/">Let’s Encrypt</a> but until recently it was not possible to import PEM files directly with .NET core 3.1. It is now possible starting with .NET 5. This feature was introduced in <a href="https://github.com/dotnet/aspnetcore/issues/4706">version 5.0.0-preview8</a>. I’ll show you how to configure it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1N89t1lZCUlJGkORIl-YuA.jpeg" /><figcaption>Image by <a href="https://pixabay.com/fr/users/skylarvision-2957633/?utm_source=link-attribution&amp;amp;utm_medium=referral&amp;amp;utm_campaign=image&amp;amp;utm_content=3344700">Skylarvision on Pixabay</a></figcaption></figure><p>As part of a little side project I’m working on, I want to host an ASP.NET Web API on my own server at home. The purpose of this API is to send push notifications to an Angular client application. It is the main reason why I had to set up SSL communication for my web site: service worker needs the communication to be secured by TLS with a valid certificate in order to allow push notications. But that’s not the subject of this article. Here I will focus on creating the certificate and loading it into Kestrel.</p><p>For the development phase I used a self-signed certificate, but now that I want to deploy it I need a real certificate certified by a certification authority. Because it is not a commercial but personal project I want to keep it entirely free. So I decided to generate a certificate with Let’s Encrypt. Until recently, ASP.Net core was only supporting .PFX files and not .PEM files provided by Let’s Encrypt. But .NET 5 has <a href="https://github.com/dotnet/aspnetcore/issues/4706">rectified the situation in version 5.0.0-preview8</a>.</p><blockquote>I could use a reverse-proxy like nginx or haproxy and let it handle the SSL stuff, but I wanted to keep it as simple as possible, so I decided to directly serve it using Kestrel.</blockquote><h3>Prerequisites</h3><ul><li>A domain name that you own. Personally I registered to <a href="http://www.duckdns.org/">Duck DNS</a> to get a free one.</li><li>An ASP.NET web site binded to your domain (to prove you are the owner of the domain)</li><li>A bash, preferably Linux ;) but Windows is OK too</li></ul><p><em>Most of Internet providers allow you to configure NAT/PAT and DynDNS, so it should not be a problem to bind your DNS to an open port on a machine in your local network.</em></p><h3>Obtain a free certificate</h3><p>Most of certification authorities sell their certificates and they are expensive. This is because the process of registering often inquires verification made by a human. Hopefully there is <a href="https://letsencrypt.org/">Let’s Encrypt</a> to the rescue. Let’s Encrypt is also a certificate authority but it is nonprofit. It aims to <a href="https://letsencrypt.org/docs/faq/#what-does-it-cost-to-use-let-s-encrypt-is-it-really-free">create a more secure and privacy-respecting Web</a>.</p><p>The first thing you have to do is to install <a href="https://certbot.eff.org/">Certbot</a>. There are many ways to do so depending on your system so I won’t go into details. The recommanded way is to use snap. Personnaly I simply made a</p><blockquote>$ sudo apt-get install certbot</blockquote><p>and it did the job.</p><p>Now that you have certbot installed you can generate a new certificate and a private key. Once again there are many ways to do this depending on your server and certbot can be configured to automatically renew your certificate before it expires. Personnaly I used the manual way and will configure autorenew later:</p><p><em>$ sudo certbot certonly — manual</em></p><p>Certbot first ask you what is your domain name and then request you to prove you are in control of it:</p><blockquote>Create a file containing just this data:</blockquote><blockquote>eZ4dZ4xUrIn7Fkyuy0Vd9giY5FoEPnm5cfGQXs_EDy0.gj7wCMjUoSpRopo7wvqqw5spi23jZdjLjtlCa00Cf2c</blockquote><blockquote>And make it available on your web server at this URL:</blockquote><blockquote><a href="http://ssldemo.dev.to/.well-known/acme-challenge/eZ4dZ4xUrIn7Fkyuy0Vd9giY5FoEPnm5cfGQXs_EDy0">http://ssldemo.dev.to/.well-known/acme-challenge/eZ4dZ4xUrIn7Fkyuy0Vd9giY5FoEPnm5cfGQXs_EDy0</a></blockquote><p>Modify your website to make the file available at the requested URL and then press ENTER. Certbot will download the file and verify that the content matches with what was requested. It will then issue a new certificate associated with your domain name and the private key used to sign the certicate. Files are created under</p><blockquote>/etc/letsencrypt/live/ssldemo.dev.to/fullchain.pem<br>/etc/letsencrypt/live/ssldemo.dev.to/privkey.pem</blockquote><p>Copy these two files in a secure place on the machine hosting your website and make sure no one can access them except your web application.</p><h3>Kestrel configuration</h3><p>Make sure your website application target at least .NET 5.0. You should have “&lt;TargetFramework&gt;net5.0&lt;/TargetFramework&gt;” in your .csproj file.</p><p>You now have to instruct Kestrel how to load your certificate. Open appsetting.json file and add a Kestrel section :</p><blockquote>“Kestrel”: {<br> “Certificates”: {<br> “Default”: {<br> “Path”: “path/to/fullchain.pem”,<br> “KeyPath”: “path/to/privkey.pem”<br> }<br> }<br> }</blockquote><blockquote>&gt; You could also use environment variables if you prefer</blockquote><p>And that’s it ! The method `ConfigureWebHostDefaults` uses Kestrel as the web server so it will use the Kestrel section to configure itself. Just make sure you load appsettings.json at startup (it is the case by default when you use `CreateDefaultBuilder`). Thus, the provided SSL certificate and the private key will be loaded and used by your server. If you don’t use `ConfigureWebHostDefaults` then ensure that you have `UseKestrel` on your webBuilder and it should be OK.</p><p>Now when you request your web API via HTTPS you should see a valid certificate in the address bar of your web browser:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/433/1*b8Lbgfi72-wk5NL660x5SA.png" /></figure><h3>Conclusion</h3><p>I wrote this article because I didn’t find many documentation on how to configure Kestrel to load PEM SSL certificate. It is my first article and I hope you will find it interesting, maybe even usefull.</p><p>If you are interested in, you can find the source code of my current side-project for which I had to load PEM SSL certificate in Kestrel.</p><ul><li><a href="https://github.com/bNobo/wind-forecast-api">https://github.com/bNobo/wind-forecast-api</a> : the web API</li><li><a href="https://github.com/bNobo/wind-forecast-website">https://github.com/bNobo/wind-forecast-website</a> : the Angular client</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7051c3f3902c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>