Integrating Ngrok with C# Applications

Integrating Ngrok with C# Applications

Introduction

Developing applications that call out to external systems is easy enough, but what happens when those external systems need to call back to your development environment? Most developers don’t have externally facing services on them as this would be a security nightmare. So, how do we develop and test applications that need an external system to call back to our development environments for things like webhooks?

This is where Ngrok comes in.

Ngrok is a great tool for developers, especially when it comes to testing and showcasing local web applications. It’s not language-specific and can be used with any technology, including C#.

This article explores how to integrate Ngrok with C# applications, allowing developers to expose their local development server to the internet.

How does it work?

You start Ngrok on your machine and specify the local port where your application is running. For instance, if your local web server is running on port 8080, you’d start Ngrok linked to this port.

Once Ngrok starts, it provides you with a public URL (e.g., https://12345.ngrok.io). This URL is unique and routes to your local development server.

Traffic that comes to the public URL provided by Ngrok is securely tunneled through Ngrok’s servers and then directed to your local server. This allows your local server to be accessed from the internet, even though it’s running on your machine.

The tunnel created by Ngrok is secure and encrypted, protecting the data transmitted through it.

Ngrok Pricing

Ngrok offers several pricing plans tailored to different user needs, ranging from a free tier to more advanced, paid options:

  1. Free Tier:
    • The free tier of Ngrok allows users to run blogs and side projects indefinitely without any cost.
    • Features include serving HTTP apps, webhooks, and APIs with a free static domain, ephemeral/random domains for quick tests, automated SSL/TLS certificates, OAuth restriction for endpoints, verification of webhook signatures for up to 500 requests per month, and traffic inspection and replay capabilities.
    • However, this tier comes with restricted bandwidth and usage​​.
  2. Pay-As-You-Go Plan:
    • This plan starts at $15 per active endpoint per month and is designed for delivering high-performance apps and APIs globally.
    • It includes features like no limits on usage (pay for what you use), the ability to bring your own branded domains, advanced load balancing, traffic logs sent to your logging provider, and OpenID Connect and SAML authentication for endpoints​​.
  3. Custom Plan:
    • The custom plan is tailored for businesses with additional security and support needs.
    • Features of this plan include mutual TLS, account governance, custom ingress (white-label Ngrok), and support with SLA (Service Level Agreement).
    • Pricing and specific details for the custom plan are provided upon request​​.

These plans cater to a range of requirements, from casual, personal use to more demanding business applications, providing flexibility and scalability based on user needs.

Use Cases in C#

  1. Webhook Testing: When developing C# applications that rely on webhook integrations (e.g., with GitHub, Slack, GoCardless, or Stripe), Ngrok allows you to test these integrations on your local machine.
  2. Client Demos: For developers working on C# web applications, Ngrok provides a quick way to share progress with clients or stakeholders without deploying the app to a staging environment.
  3. Cross-Device Testing: Testing how a C# web app renders on various devices can be simplified with Ngrok, as it makes the local server accessible on different devices connected to the internet.

Setting Up Ngrok for a C# Application

  1. Download Ngrok: Visit Ngrok’s website and download the appropriate version for your operating system.
  2. Unzip and Run Ngrok: Extract the downloaded file and run Ngrok from the command line. Specify the port your C# application runs on (commonly 5000 or 5001 for ASP.NET Core apps) as shown in the code block below.
  3. Configure C# Application: No specific configuration is needed in your C# application. Just ensure it’s running on the port you’ve tunnelled with Ngrok.
  4. Accessing the Public URL: Once Ngrok starts, it provides a public URL (e.g., https://12345.ngrok.io). This URL routes to your local development server.

ngrok http 5001

Integration Tips

  • Securing Ngrok Tunnel: Use Ngrok’s authentication features to secure your tunnel, especially when exposing sensitive applications.
  • Testing Webhooks: For webhook testing, provide the Ngrok URL in the webhook configuration on the third-party service.
  • Handling HTTPS: Ngrok tunnels support HTTPS, which is essential if your application requires secure connections.
  • Environment Variables: You can programmatically set the Ngrok URL in your application’s environment variables for dynamic configurations, especially useful in webhook scenarios.

Automating Ngrok in C#

For advanced integration, you might want to control Ngrok directly from your C# code:

Start Ngrok with C#: Use System.Diagnostics.Process to start an Ngrok process.

using System.Diagnostics; 

ProcessStartInfo startInfo = new ProcessStartInfo() 
{ 
    FileName = "path/to/ngrok", 
    Arguments = "http 5001", 
    UseShellExecute = true 
}; 

Process.Start(startInfo);

Fetching Ngrok URL: You can fetch the Ngrok URL by calling Ngrok’s API.

using (HttpClient clientnew HttpClient()) 
{ 
    client.BaseAddressnewUri("http://127.0.0.1:4040"); 
    
    HttpResponseMessage responseawaitclient.GetAsync("/api/tunnels"); 
    string responseDataawaitresponse.Content.ReadAsStringAsync(); 
    
    // Parse responseData to extract the public URL 
}

Conclusion

Integrating Ngrok with C# applications is straightforward and does not require direct modifications to the application code.

It offers an effective solution for testing, showcasing, and developing C# web applications that interact with external services or need to be accessible over the public internet.

Whether it’s for demonstrating progress to clients or testing webhooks, Ngrok proves to be an indispensable tool for C# developers.

Stephen

Hi, my name is Stephen Finchett. I have been a software engineer for over 30 years and worked on complex, business critical, multi-user systems for all of my career. For the last 15 years, I have been concentrating on web based solutions using the Microsoft Stack including ASP.Net, C#, TypeScript, SQL Server and running everything at scale within Kubernetes.