Using ngrok with IIS Express and ASP.NET Core

Using ngrok with IIS Express and ASP.NET Core

Sometimes it can be helpful to quickly make the version of a project which is running on your development machine available to others. This could be for webhook integrations, demos, or some other reason. One of the tools you can use to achieve this is ngrok.

ngrok creates a secure tunnel and expose the web server running on your machine (usually IIS Express for .NET Projects if you’re working on Windows). As a result, you can give a publicly accessible URL (such as https://a1c2e3g4i5kl.ngrok.io) to others which will allow them to access the site running on your development machine.

Find the IIS Express URL

The first step we need to take is to find the local URL which IIS Express setup when we created our project. We can do this through a few different ways.

  •  Run the project and copy the root URL from the web browser.
  • Viewing the URL in the site menu from the IIS Express system tray icon.

For the examples on this page, I am going to use the local address https://localhost:44311.

After we’ve got the local URL for our site we can move on to the next step.

Creating a tunnel to IIS Express

According to the ngrok documentation on local servers, we should be able to expose the appropriate site by entering the following command.

ngrok http https://localhost:44311

However, if you use this command, you will receive a “Bad Request – Invalid Hostname” error message when you attempt to access the site through the provided ngrok URL. This is because we will need to rewrite the host header to be appropriate for the IIS Express site we wish to expose.

Rewriting the Host header

When a request comes in from a web browser, IIS Express uses the host header to determine which site it should load. By accessing the site through the public URL, the host header sent from the web browser will not match the one IIS Express is expecting.

Thankfully, the ngrok includes documentation for rewriting the host header. We can do this by appending –host-header=rewrite to our previous command.

ngrok http -host-header=rewrite https://localhost:44311

This will make ngrok rewrite the host header to match the local address which will let IIS Express know which site to load.

Summary

To use ngrok to expose an ASP.NET Core web app on IIS Express you need to specify the local address and set the host header to be rewritten to match.

Modify the following command by changing the address to match your site’s local address. This will allow you to access that IIS Express site through the ngrok tunnel.

ngrok http -host-header=rewrite https://localhost:44311