Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Products
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET Core
  4. Implementing IP Whitelisting and Blacklisting Middleware in ASP.NET Core

Implementing IP Whitelisting and Blacklisting Middleware in ASP.NET Core

Date- Jun 10,2026 290
aspnetcore middleware

Overview

IP Whitelisting and Blacklisting are essential security mechanisms used to control access to applications based on the originating IP addresses of requests. Whitelisting allows only specified IP addresses to access the application, while blacklisting denies access to specified IP addresses. Each method addresses different security requirements and can be used in conjunction with other security practices to prevent unauthorized access and mitigate potential threats.

These techniques are particularly relevant in environments where applications are exposed to the public internet. For example, a financial institution may only allow requests from known IP addresses, such as those of its branches or trusted partners. Conversely, a company may blacklist IP addresses associated with malicious activity or known bots to protect its resources. In both cases, implementing IP filtering can significantly reduce the attack surface of an application.

Prerequisites

  • ASP.NET Core SDK: Ensure you have the .NET SDK installed to create and run ASP.NET Core applications.
  • Basic C# Knowledge: Familiarity with C# programming is necessary to understand middleware implementation.
  • Understanding Middleware: A grasp of how middleware works in ASP.NET Core is essential for effective implementation.
  • IDE: An integrated development environment like Visual Studio or Visual Studio Code for coding and debugging.

Creating Middleware for IP Whitelisting

To implement IP whitelisting in an ASP.NET Core application, we need to create custom middleware that checks incoming requests against a list of allowed IP addresses. If the request's IP address is not in the whitelist, the middleware should return an unauthorized response.

using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;

public class IpWhitelistingMiddleware
{
    private readonly RequestDelegate _next;
    private readonly HashSet _whitelistedIps;

    public IpWhitelistingMiddleware(RequestDelegate next, IEnumerable whitelistedIps)
    {
        _next = next;
        _whitelistedIps = new HashSet(whitelistedIps);
    }

    public async Task InvokeAsync(HttpContext context)
    {
        var remoteIp = context.Connection.RemoteIpAddress;
        if (!_whitelistedIps.Contains(remoteIp))
        {
            context.Response.StatusCode = StatusCodes.Status403Forbidden;
            await context.Response.WriteAsync("Forbidden: Your IP is not allowed.");
            return;
        }
        await _next(context);
    }
}

The above code defines a middleware class called IpWhitelistingMiddleware. It takes a RequestDelegate and a collection of whitelisted IP addresses as parameters. The constructor initializes the middleware, storing the allowed IPs in a HashSet for efficient lookups.

The InvokeAsync method retrieves the remote IP address of the incoming request using context.Connection.RemoteIpAddress. It checks if the IP is in the whitelist; if not, it sets the response status code to 403 Forbidden and sends a message indicating access is denied. If the IP is allowed, it calls the next middleware in the pipeline using await _next(context).

Registering the Middleware

To use the middleware, we need to register it in the Startup.cs file of the ASP.NET Core application. This involves configuring the middleware in the Configure method.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    var whitelistedIps = new List
    {
        IPAddress.Parse("192.168.1.1"),
        IPAddress.Parse("203.0.113.5")
    };

    app.UseMiddleware(whitelistedIps);
    // Other middleware registrations
}

In this code snippet, we define a list of whitelisted IPs and register the IpWhitelistingMiddleware using app.UseMiddleware. This ensures that all incoming requests are checked against the specified IP addresses before proceeding to the next middleware.

Creating Middleware for IP Blacklisting

IP blacklisting middleware works similarly to whitelisting but denies access to specific IP addresses instead. To implement this, we create another middleware class that checks incoming requests against a blacklist.

using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;

public class IpBlacklistingMiddleware
{
    private readonly RequestDelegate _next;
    private readonly HashSet _blacklistedIps;

    public IpBlacklistingMiddleware(RequestDelegate next, IEnumerable blacklistedIps)
    {
        _next = next;
        _blacklistedIps = new HashSet(blacklistedIps);
    }

    public async Task InvokeAsync(HttpContext context)
    {
        var remoteIp = context.Connection.RemoteIpAddress;
        if (_blacklistedIps.Contains(remoteIp))
        {
            context.Response.StatusCode = StatusCodes.Status403Forbidden;
            await context.Response.WriteAsync("Forbidden: Your IP is blacklisted.");
            return;
        }
        await _next(context);
    }
}

This code defines the IpBlacklistingMiddleware class that initializes with a list of blacklisted IP addresses. The InvokeAsync method checks if the incoming request's remote IP is in the blacklist. If so, it returns a 403 Forbidden response; otherwise, it calls the next middleware.

Registering the Blacklisting Middleware

Similar to whitelisting, we must register the blacklisting middleware in the Startup.cs file.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    var blacklistedIps = new List
    {
        IPAddress.Parse("192.168.1.100"),
        IPAddress.Parse("203.0.113.10")
    };

    app.UseMiddleware(blacklistedIps);
    // Other middleware registrations
}

In this case, we define a list of blacklisted IPs and register the IpBlacklistingMiddleware in the pipeline. Requests from any of the blacklisted IP addresses will be denied access.

Edge Cases & Gotchas

When implementing IP whitelisting and blacklisting, there are several edge cases and pitfalls to consider. One common issue arises when dealing with proxies or load balancers that may alter the RemoteIpAddress. Ensure that your application appropriately handles forwarded headers if it is behind a proxy.

app.UseForwardedHeaders(new ForwardedHeadersOptions
{
    ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});

Another potential pitfall is the use of CIDR notation for IP ranges. In such cases, you will need to implement additional logic to parse and check whether an IP falls within a specified range, which can complicate your middleware implementation.

Performance & Best Practices

Performance is a critical consideration when implementing IP filtering. Using a HashSet for storing IP addresses allows for O(1) average time complexity for lookups, which is essential for maintaining responsiveness in high-traffic applications. Always prefer using collections optimized for search operations.

Another best practice is to cache the IP lists, especially for large applications. This approach minimizes the overhead of repeatedly checking against a potentially large list of IPs. You can utilize in-memory caching or distributed caching solutions depending on your deployment architecture.

Real-World Scenario

Let's consider a real-world scenario where we create an ASP.NET Core web application that implements both whitelisting and blacklisting. This application will allow only requests from a predefined set of IP addresses while blocking known malicious IPs.

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // Add services for MVC or other services
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        var whitelistedIps = new List
        {
            IPAddress.Parse("192.168.1.1"),
            IPAddress.Parse("203.0.113.5")
        };

        var blacklistedIps = new List
        {
            IPAddress.Parse("192.168.1.100"),
            IPAddress.Parse("203.0.113.10")
        };

        app.UseMiddleware(blacklistedIps);
        app.UseMiddleware(whitelistedIps);

        app.UseRouting();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapGet("/", async context =>
            {
                await context.Response.WriteAsync("Welcome to the secure application!");
            });
        });
    }
}

This example demonstrates how to set up a simple web application where both IP whitelisting and blacklisting middleware are registered. The application responds with a welcome message only to requests from IPs that are whitelisted and not blacklisted.

Conclusion

  • IP whitelisting and blacklisting are vital techniques for securing ASP.NET Core applications.
  • Custom middleware can effectively handle IP filtering based on predefined lists.
  • Performance considerations are crucial; using appropriate data structures can enhance responsiveness.
  • Understanding edge cases, such as handling proxies and CIDR notation, can prevent common pitfalls.
  • Real-world scenarios demonstrate the practical application of these techniques in securing web applications.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Understanding Middleware in ASP.NET Core: A Comprehensive Guide
Mar 16, 2026
Implementing API Key Authentication Middleware in ASP.NET Core Web API
Jun 10, 2026
Integrating LinkedIn OAuth in ASP.NET Core for Professional Login
May 01, 2026
Implementing GitHub OAuth Integration in ASP.NET Core for Seamless User Login
Apr 30, 2026
Previous in ASP.NET Core
Protecting ASP.NET Core Web API Endpoints with JWT Bearer Authent…
Next in ASP.NET Core
Preventing Sensitive Data Exposure in ASP.NET Core API Responses …
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    CWE-269: Improper Privilege Management - Implementing the … 362 views
  • 2
    Send Email With HTML Template And PDF Using ASP.Net C# 17,226 views
  • 3
    Building Custom Bedrock Add-Ons with JavaScript: A Complet… 1,940 views
  • 4
    Error-An error occurred while processing your request in .… 11,965 views
  • 5
    Elasticsearch Integration in ASP.NET Core - Full-Text Sear… 245 views
  • 6
    Fix Gemini API Error 429: Quota Exceeded on Free Tier (Sys… 833 views
  • 7
    Integrating Google reCAPTCHA v3 in ASP.NET Core for Seamle… 614 views

On this page

🎯

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • How to Encrypt and Decrypt Password in Asp.Net 26685 views
  • Exception Handling Asp.Net Core 21722 views
  • HTTP Error 500.31 Failed to load ASP NET Core runtime 21180 views
  • How to implement Paypal in Asp.Net Core 20129 views
  • Task Scheduler in Asp.Net core 18204 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1780
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Products
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor