Securing your aspnet core web application using Kestrel

So you want to secure the login page of your web application or maybe you want to enable Facebook login for your application which requires you use a secured web page. Well, the second scenario got me stuck with figuring out how to setup up https for dotnet core 1.0 since all my old tricks didn not work but yeah if I just wanted to use Visual Studio and IIS I guess that would be fine but no I always want cross-platform were possible.
So here's what it would take to set it up yourself. You will need a .pfx file in hand though. Windows users can generate a self signed certificate using IIS.

  • Create a new aspnet project using yo and in the options select the Web Application Yeoman options
  • Open your project.json file and "Microsoft.AspNetcore.Server.Kestrel.Https": "1.0.1". Let me say at this point that the version of this dependency must match the version of Kestrel you have installed (at least as at the time of writing). You can scroll through the file to find Kestrel and not the version.
  • Now go over to the Program.cs which is basically the entry point to your dotnet core application and add .UseUrls("https://localhost:5001", "http://localhost:5000").
public class Program  
{
    public static void Main(string[] args)
    {
        var config = new ConfigurationBuilder()
            .AddCommandLine(args)
            .AddEnvironmentVariables(prefix: "ASPNETCORE_")
            .Build();

        var host = new WebHostBuilder()
            .UseConfiguration(config)
            .UseKestrel()
            .UseUrls("https://localhost:5001", "http://localhost:5000")
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}
  • Go to your Startup.cs and add a new property public IConfiguration Congfiguration {get; set;} and a new string variable certfile to hold the certficate file path.
  • In the contructor method assign the path of your certificate to the certfile variable like this
public Startup(IHostingEnvironment env)  
{
    certfile = Path.Combine(env.ContentRootPath, "coremvc.pfx");
}
  • In the ConfigureServices method:
services.Configure<KestrelServerOptions>(options =>  
    {
        options.UseHttps(certfile,"jollof");
        options.UseConnectionLogging();
    });

You should be all set at this point. Your final Startup class should look like this:

using System.IO;  
using Microsoft.AspNetCore.Builder;  
using Microsoft.AspNetCore.Hosting;  
using Microsoft.AspNetCore.Http;  
using Microsoft.AspNetCore.Server.Kestrel;  
using Microsoft.Extensions.Configuration;  
using Microsoft.Extensions.DependencyInjection;  
using Microsoft.Extensions.Logging;

namespace MVCCoreHTTPS  
{
    public class Startup
    {
        public string certfile { get; set; }
        public Startup(IHostingEnvironment env)
        {
            certfile = Path.Combine(env.ContentRootPath, "coremvc.pfx");
        }

        public IConfiguration Configuration { get; set; }
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvcCore();
            services.Configure<KestrelServerOptions>(options =>
            {
                options.UseHttps(certfile,"jollof"); // jollof is my certificate password, what is yours? :P
                options.UseConnectionLogging();
            });
        }

       public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvcWithDefaultRoute();
            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World");
            });
        }
    }
}

Now that you are here, if you've found this tutorial useful, don't be stingy but share with others through whatever means possible. Click here to access the github repo and star it for future changes and please leave a comment for any reason at all. :D
Star Fork Download

Enjoy!

Show Comments