PROWARE technologies
PROWARE technologies

Change the Listening Port on ASP.NET Core 3.0 Kestrel Server and Configure for HTTPS

This article requires ASP.NET Core 3.0 even though it may work with other versions.

The Kestrel server is used by Linux, for example.

By modifying one line in the Program.cs file, it is possible to change the default port of 5000 that the Kestrel server is listening on.

Here is an example Program.cs file. It now uses the UseUrls() method which takes an array of strings. This array could probably also be saved as a JSON file and then loaded everytime the server is started.

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace ProjectName
{
	public class Program
	{
		public static void Main(string[] args)
		{
			CreateHostBuilder(args).Build().Run();
		}

		public static IHostBuilder CreateHostBuilder(string[] args) =>
			Host.CreateDefaultBuilder(args)
				.ConfigureWebHostDefaults(webBuilder =>
				{
					webBuilder.UseStartup<Startup>().UseUrls(new[] { "http://0.0.0.0:5001" }); // now the Kestrel server will listen on port 5001!
				});
	}
}

Alternatively, use the command line arguments to specify the ports to listen on.

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace ProjectName
{
	public class Program
	{
		public static void Main(string[] args)
		{
			CreateHostBuilder(args).Build().Run();
		}

		public static IHostBuilder CreateHostBuilder(string[] args) =>
			Host.CreateDefaultBuilder(args)
				.ConfigureWebHostDefaults(webBuilder =>
				{
					webBuilder.UseStartup<Startup>().UseUrls(args); // now the Kestrel server will listen to the URLs specified in the arguments
				});
	}
}

Issue the commmand dotnet ProjectName.dll http://0.0.0.0:5001 http://0.0.0.0:8080 to make the Kestrel server listen on both ports 5001 and 8080.

HTTPS Kestrel Server

Buy a certificate online or create a self-signed one.

Create Self-signed Certificate

On Windows with the .NET Core SDK installed: dotnet dev-certs https -ep %USERPROFILE%\Desktop\cert.pfx -p password

Configure the Kestrel Server to Use the Certificate

Here is the Program.cs file:

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace ProjectName
{
	public class Program
	{
		public static void Main(string[] args) // Three arguments should be passed: the port to listen on, the path to the "cert.pfx" file, and the password used for the cert
		{
			CreateHostBuilder(args).Build().Run();
		}

		public static IHostBuilder CreateHostBuilder(string[] args) =>
			Host.CreateDefaultBuilder(args)
				.ConfigureWebHostDefaults(webBuilder =>
				{
					webBuilder.UseStartup<Startup>().UseKestrel(options =>
					{
						int port = 5443; // port defaults to 5443
						if (args.Length > 0)
							int.TryParse(args[0], out port); // args #1 is port
						if (args.Length > 2)
						{
							options.ListenAnyIP(port, listenOptions =>
							{
								listenOptions.UseHttps(args[1], args[2]); // args #2 and #3 are path to cert.pfx and its password
							});
						}
						else
						{
							options.ListenAnyIP(port);
						}
					});
				});
	}
}

Here is the Startup.cs file:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace ProjectName
{
	public class Startup
	{
		public Startup(IConfiguration configuration)
		{
			Configuration = configuration;
		}

		public IConfiguration Configuration { get; }

		// This method gets called by the runtime. Use this method to add services to the container.
		public void ConfigureServices(IServiceCollection services)
		{
			services.AddControllersWithViews();
		}

		// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
		public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
		{

			System.IO.Directory.SetCurrentDirectory(env.ContentRootPath); // NOTE: MAKE SURE TO SET THE CURRENT DIRECTORY TO THE CONTENT ROOT PATH

			if (env.IsDevelopment())
			{
				app.UseDeveloperExceptionPage();
			}
			else
			{
				app.UseExceptionHandler("/Home/Error");
				// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
				app.UseHsts();
			}
			app.UseHttpsRedirection();
			app.UseStaticFiles();

			app.UseRouting();

			app.UseAuthorization();

			app.UseEndpoints(endpoints =>
			{
				endpoints.MapControllerRoute(
					name: "default",
					pattern: "{controller=Home}/{action=Index}/{id?}");
			});
		}
	}
}

Tutorial and Coding Video

https://youtu.be/fUZQdO5yVfI