.NET Core  

How to use Ngrok in ASP.NET Core

Introduction

When building modern web applications, developers often face a common challenge: testing locally developed webhooks, APIs, or third-party integrations that require a publicly accessible URL.

Ngrok solves this problem elegantly by creating a secure tunnel to your local server, instantly exposing it to the internet. In this article, we’ll understand what Ngrok is, why it’s useful, and how to integrate it with an ASP.NET Core application, complete with a working example.

What is Ngrok?

Ngrok is a reverse proxy tool that securely tunnels local ports to a public URL. It helps developers:

  • Test webhooks from services like Stripe, PayPal, or Twilio.
  • Share local development sites for demos.
  • Test mobile apps against a local backend without deploying.

How Ngrok Works

Ngrok works in 3 simple steps:

  1. It listens on a local port (e.g., your ASP.NET Core app running on https://localhost:5001).
  2. It creates a secure tunnel to the Ngrok servers.
  3. It provides you with a unique public URL (e.g., https://a1b2c3.ngrok.io), which forwards incoming requests to your local server.

Prerequisites

Before starting, ensure you have:

  • ASP.NET Core project ready (can be a new Web API).
  • Ngrok installed (Download from https://ngrok.com and add to your system PATH).

Step-by-Step Example

Let’s build a simple ASP.NET Core API and expose it with Ngrok.

1. Create a New ASP.NET Core Web API

Use the .NET CLI to scaffold a new API.

dotnet new webapi -n NgrokDemoApi
cd NgrokDemoApi

This creates a project with a sample WeatherForecast controller.

2. Run Your Application Locally

Run the API.

dotnet run

By default, it runs on:

  • https://localhost:5001
  • http://localhost:5000

3. Start Ngrok Tunnel

Open a new terminal (don’t stop your API) and run.

ngrok http 5000

Note: If your API runs on HTTPS (5001), You can tunnel HTTPS directly or use HTTP for simplicity during development.

After running the command, Ngrok displays output like this.

Forwarding                    http://a1b2c3.ngrok.io -> http://localhost:5000
Forwarding                    https://a1b2c3.ngrok.io -> http://localhost:5000

This URL (https://a1b2c3.ngrok.io) is now public!

4. Test Your Public Endpoint

Open a browser or Postman and navigate to.

https://a1b2c3.ngrok.io/weatherforecast

You should see the same response as http://localhost:5000/weatherforecast.

Practical Use Case: Receiving Webhooks

Let’s say you want to receive a webhook from Stripe for payment events. Stripe needs a public callback URL. With Ngrok:

  1. Run your ASP.NET Core app locally.
  2. Use Ngrok to get a public URL.
  3. Configure Stripe’s webhook URL as https://a1b2c3.ngrok.io/api/webhook.

Done! No deployment needed during development.

Tips for Using Ngrok Effectively

  • Authenticated tunnels: Create an Ngrok account to get an AuthToken and access reserved subdomains.
  • Custom subdomains: Pro users can reserve subdomains, such as https://myapp.ngrok.io.
  • Inspect requests: Ngrok provides a web interface at http://localhost:4040 to see and replay incoming requests.

Example launchSettings.json

In ASP.NET Core, check your launchSettings.json to confirm which port your app uses:

{
  "profiles": {
    "NgrokDemoApi": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Ensure you tunnel the same HTTP port that Ngrok points to.

Conclusion

Ngrok is a powerful tool that can save hours when developing, testing, and demonstrating applications that need to interact with external services. It bridges the gap between your local machine and the outside world securely and instantly.