{"id":3464,"date":"2017-02-02T01:04:49","date_gmt":"2017-02-02T06:04:49","guid":{"rendered":"https:\/\/www.thesslstore.com\/blog\/?p=3464"},"modified":"2023-03-31T12:24:41","modified_gmt":"2023-03-31T16:24:41","slug":"implement-ssl-kestrel-asp-net-core","status":"publish","type":"post","link":"https:\/\/www.thesslstore.com\/blog\/implement-ssl-kestrel-asp-net-core\/","title":{"rendered":"How to Implement SSL on Kestrel\/ ASP.net core"},"content":{"rendered":"<h2>A Step-by-Step Guide on Configuring HTTPS on Kestrel\/ASP.net<\/h2>\n<p>Kestrel is a web server that comes included with ASP.net Core new-project templates as a default. Kestrel represents a cross-platform web server based on libuv\u2014a cross-platform asynchronous I\/O library.<\/p>\n<p>Kestrel is relatively new, as October 27, 2016, Microsoft was still suggesting you use IIS, Nginx or Apache as a reverse proxy server for edge deployments as a result of security concerns:<\/p>\n<blockquote><p>Kestrel is relatively new and does not yet have a full complement of defenses against attacks. This includes but isn&#8217;t limited to appropriate timeouts, size limits, and concurrent connection limits. For more information about when to use Kestrel with a reverse proxy, see <strong>Kestrel<\/strong><\/p><\/blockquote>\n<p>Today we\u2019re going to discuss how to implement SSL on Kestrel and enable HTTPS connections, adding a much needed layer of security to the fledgling server.<\/p>\n<p>To implement SSL and configure the server for HTTPS, you\u2019ll need to start by acquiring an SSL certificate. This a simple process, you\u2019ll just need to:<\/p>\n<ol>\n<li>Select and purchase an SSL certificate<\/li>\n<li>Generate a CSR and a public\/private key-pair<\/li>\n<li>Send the public key and your CSR to the Certificate Authority<\/li>\n<li>Undergo validation<\/li>\n<li>Receive your SSL Certificate and any intermediates that accompany it<\/li>\n<\/ol>\n<span style=\"--tl-form-height-m:150.25px;--tl-form-height-t:121.4583px;--tl-form-height-d:121.4583px;\" class=\"tl-placeholder-f-type-shortcode_12753 tl-preload-form\"><span><\/span><\/span>\n<p>After you\u2019ve done all this, make sure you have provided a friendly name for the certificate and store, and have the pfx file handy.<\/p>\n<p>Modify the project.json file to add a reference to Kestrel.https nuget package.<\/p>\n<p>&#8220;dependencies&#8221;: {<br \/>\n&#8220;Microsoft.AspNet.Server.Kestrel&#8221;: &#8220;1.0.0-rc1-final&#8221;,<br \/>\n&#8220;Microsoft.AspNet.IISPlatformHandler&#8221;: &#8220;1.0.0-rc1-final&#8221;,<br \/>\n&#8220;Microsoft.AspNet.Diagnostics&#8221;: &#8220;1.0.0-rc1-final&#8221;,<br \/>\n&#8220;Microsoft.AspNet.Mvc&#8221;: &#8220;6.0.0-rc1-final&#8221;,<br \/>\n&#8220;Microsoft.Extensions.Logging.Console&#8221;: &#8220;1.0.0-rc1-final&#8221;,<br \/>\n&#8220;Microsoft.AspNet.Server.Kestrel.Https&#8221;: &#8220;1.0.0-rc1-final&#8221;<br \/>\n}<\/p>\n<p>After that, you\u2019ll need to modify the startup file, configure() method to use the certificate.<\/p>\n<p>public void Configure(IApplicationBuilder app,<br \/>\nIHostingEnvironment env,<br \/>\nIApplicationEnvironment appEnv,<br \/>\nILoggerFactory loggerFactory)<br \/>\n{<br \/>\nloggerFactory.AddConsole();<br \/>\nvar pfxFile = Path.Combine(appEnv.ApplicationBasePath, &#8220;Sample.pfx&#8221;);<br \/>\nX509Certificate2 certificate = new X509Certificate2(pfxFile, &#8220;Password&#8221;);<br \/>\napp.Use(ChangeContextToHttps);<br \/>\napp.UseKestrelHttps(certificate);<br \/>\napp.UseDeveloperExceptionPage();<br \/>\napp.UseMvcWithDefaultRoute();<br \/>\n}<\/p>\n<p>Now, make sure not to hard code your certificate password, use the secrets API instead.<\/p>\n<p>The method below is a work-around for the <a href=\"https:\/\/github.com\/aspnet\/KestrelHttpServer\/issues\/454\" target=\"_blank\" rel=\"noopener\">454 defect<\/a>.<\/p>\n<p><strong><em>Note:<\/em><\/strong> This has been fixed in RC2.<\/p>\n<p>private static RequestDelegate ChangeContextToHttps(RequestDelegate next)<br \/>\n{<br \/>\nreturn async context =&gt;<br \/>\n{<br \/>\ncontext.Request.Scheme = &#8220;https&#8221;;<br \/>\nawait next(context);<br \/>\n};<br \/>\n}<\/p>\n<p>Next up you will need to modify the <strong>project.json<\/strong> and change the URL:<\/p>\n<p>&#8220;commands&#8221;: {<br \/>\n&#8220;web&#8221;: &#8220;Microsoft.AspNet.Server.Kestrel &#8211;server.urls https:\/\/*:5004&#8221;<br \/>\n}<\/p>\n<p>Finally, your last order of business is to remove \u201cdnxcore50\u201d from the frameworks. <strong>Kestrel.https<\/strong> is not supported in <strong>dnxcore<\/strong>.<\/p>\n<p>Congratulations, your <strong>Kestrel server is now optimized for HTTPS.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A Step-by-Step Guide on Configuring HTTPS on Kestrel\/ASP.net Kestrel is a web server that comes included with ASP.net Core new-project templates as a default. Kestrel represents a cross-platform web server&#8230;<\/p>\n","protected":false},"author":1,"featured_media":3465,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":"","tve_updated_post":"","tve_custom_css":"","tve_user_custom_css":"","tve_globals":{},"tcb2_ready":0,"tcb_editor_enabled":0,"tve_landing_page":"","_tve_header":"","_tve_footer":""},"categories":[25],"tags":[377,378],"class_list":["post-3464","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ssl-certificates","tag-how-to-implement-ssl-on-kestrel","tag-ssl-on-kestrel-asp-net-core","post-with-tags"],"views":27511,"jetpack_featured_media_url":"https:\/\/www.thesslstore.com\/blog\/wp-content\/uploads\/2017\/02\/ASP.NETcore1.jpg","_links":{"self":[{"href":"https:\/\/www.thesslstore.com\/blog\/wp-json\/wp\/v2\/posts\/3464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.thesslstore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thesslstore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thesslstore.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thesslstore.com\/blog\/wp-json\/wp\/v2\/comments?post=3464"}],"version-history":[{"count":0,"href":"https:\/\/www.thesslstore.com\/blog\/wp-json\/wp\/v2\/posts\/3464\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.thesslstore.com\/blog\/wp-json\/wp\/v2\/media\/3465"}],"wp:attachment":[{"href":"https:\/\/www.thesslstore.com\/blog\/wp-json\/wp\/v2\/media?parent=3464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thesslstore.com\/blog\/wp-json\/wp\/v2\/categories?post=3464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thesslstore.com\/blog\/wp-json\/wp\/v2\/tags?post=3464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}