Sibeesh Passion

Top Menu

  • Home
  • Search
  • About
  • Privacy Policy

Main Menu

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Home
  • Search
  • About
  • Privacy Policy

logo

Sibeesh Passion

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment

  • Build, Deploy, Configure CI &CD Your Static Website in 5 mins

  • Post Messages to Microsoft Teams Using Python

  • Get Azure Blob Storage Blob Metadata Using PowerShell

  • Deploy .net 6 App to Azure from Azure DevOps using Pipelines

.NET
Home›.NET›.NET 8 New and Efficient Way to Check IP is in Given IP Range

.NET 8 New and Efficient Way to Check IP is in Given IP Range

By SibeeshVenu
October 13, 2023
0
0
Share:
DOTNET 8 New and Efficient Way to Check IP is in Given IP Ranges

In our last post we have seen how we can restrict the access to your application by implementing IP restrictions using ActionFilterAttribute but we were checking the IP addresses and was missing a feature to support the IP Ranges. The good news is that the .NET 8 has a new and efficient way to do this. Here in this post, we will see how we can implement the same. Let’s begin.

Source Code

You can also see the codes in this repository.

Enablling .NET8 for Your Application

As you might have already guessed, the first step is to download the .NET8 from here. Let’s now change the TargetFramework in our both DotNetIpFilter.csproj and DotNetIpFilter.Test.csproj files. You can get the application code from the repositroy mentioned above.

<TargetFramework>net8.0</TargetFramework>

You will also need to update your Microsoft.AspNetCore.OpenApi package to .NET8.

Enable Preview .NET 8 in Visual Studio 2022

If you are trying to create a new application using Visual Studio 200, you may be missing an option to chose the .NET8 framework, to mak sure to get this option, do the following.

1. Update your Visual Studio 2022

2. Go to Tools -> Manage Preview Features

.NET 8 Manage Preview Features

3. Select checkbox Use preview of the .NET SDK (requires restart)

4. Make sure to restart after enabling this

Use .NET Previews of the .NET SDK

Use .NET8 Contains Method from IPNetwork

Let’s change our IpActionFilter.cs file code as below, to use the Contains method from .NET8 IPNetwork class.

In the code above the variable addressRange is a string that represents IP Address Range and ipAddressToCheck is the IP Address to check. The code for the IPNetwork is here. You can learn more about this IPNetwork.Contains(IPAddress) method here. Make sure to select the version .NET 8 for the doc. If you can’t use .Net 8, you can implement your own class by taking the code from here.

I also have a detailed StackOverflow answer here on this topic.

StackOverflow answer

Test IP Filtering with Ranges

Now, it is time to test our new implementation. Go to the IpFilterService.cs file and then edit the GetAdminSafeIpList method. Assign a few IP address ranges to the variable ipListArray, remember this is where you get a list of IP addresses and ranges from any of your middleware service using an async function. To make it simpler, I am assigning some of the IP addresses and ranges to that variable manually.

When you run the application you can see that the addresses and the ranges we set is getting validated in our OnActionExecuting method in IpActionFilter.cs.

Compare IP Addresses and Ranges

The Contains method retruns true if the given IPAddress is part of the IP network. Otherwise, returns false.

Returns false when IP Address is not in Ranges

If you inspect the Contains method, you can see the code as below. This works with both IPV4 and IPV6.

Conclusion

In this post we have seen how easily we can check an IP address is in a given IP address ranges or not. Before this .Net8 release, we had to have our own implementaion, but now it is so easy and less codes. Less code is less maintenance, right. The .NET8 has many features just like this, checkout here.

About the Author

I am yet another developer who is passionate about writing and sharing knowledge. I have written more than 500 blogs on my blog. If you like this content, consider following me here,

  • GitHub
  • medium
  • Twitter

Your turn. What do you think?

Thanks a lot for reading. Did I miss anything that you may think is needed in this article? Could you find this post useful? Kindly do not forget to share your feedback.

Tags.NET8DOTNET8How to check IP is in IP RangesIP in IP RangesIPNetwork Class DOTNET8
Previous Article

Async Client IP safelist for Dot NET

Next Article

Easily move data from one COSMOS DB ...

0
Shares
  • 0
  • +
  • 0
  • 0
  • 0

SibeeshVenu

I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer.

Related articles More from author

  • Creating Entity
    .NETASP.NET

    Working with API help page controller action description in Web API

    May 12, 2016
    By SibeeshVenu
  • LINQ Basic to Advanced Index View
    .NETASP.NETC#MVC

    LINQ Basic To Advanced – MVC Demo Application

    May 15, 2017
    By SibeeshVenu
  • .NETAsp.Net CoreAzure

    Asp.Net Core Windows Service Task Quartz.Net With Database

    August 29, 2019
    By SibeeshVenu
  • .NETASP.NET

    Determine Which Browser Your Application is Running In

    June 17, 2015
    By SibeeshVenu
  • .NETAzureUWP

    UWP Application Signin, Release, Distribute, Deploy With Azure DevOps Pipeline

    November 3, 2019
    By SibeeshVenu
  • .NETASP.NETC#JsonMicrosoft ADOMDMicrosoft Technologies

    Convert CellSet to HTML Table and From HTML to JSON and to Array

    October 29, 2014
    By SibeeshVenu
0

My book

Asp Net Core and Azure with Raspberry Pi Sibeesh Venu

YouTube

MICROSOFT MVP (2016-2022)

profile for Sibeesh Venu - Microsoft MVP

Recent Posts

  • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment
  • Build, Deploy, Configure CI &CD Your Static Website in 5 mins
  • Easily move data from one COSMOS DB to another
  • .NET 8 New and Efficient Way to Check IP is in Given IP Range
  • Async Client IP safelist for Dot NET
  • Post Messages to Microsoft Teams Using Python
  • Get Azure Blob Storage Blob Metadata Using PowerShell
  • Deploy .net 6 App to Azure from Azure DevOps using Pipelines
  • Integrate Azure App Insights in 1 Minute to .Net6 Application
  • Azure DevOps Service Connection with Multiple Azure Resource Group

Tags

Achievements (35) Angular (14) Angular 5 (7) Angular JS (15) article (10) Article Of The Day (13) Asp.Net (14) Azure (65) Azure DevOps (10) Azure Function (10) Azure IoT (7) C# (17) c-sharp corner (13) Career Advice (11) chart (11) CSharp (7) CSS (7) CSS3 (6) HighChart (10) How To (9) HTML5 (10) HTML5 Chart (11) Interview (6) IoT (11) Javascript (10) JQuery (82) jquery functions (9) JQWidgets (15) JQX Grid (17) Json (7) Microsoft (8) MVC (20) MVP (9) MXChip (7) News (18) Office 365 (7) Products (10) SQL (20) SQL Server (15) Visual Studio (10) Visual Studio 2017 (7) VS2017 (7) Web API (12) Windows 10 (7) Wordpress (9)
  • .NET
  • Achievements
  • ADO.NET
  • Android
  • Angular
  • Arduino
  • Article Of The Day
  • ASP.NET
  • Asp.Net Core
  • Automobile
  • Awards
  • Azure
  • Azure CDN
  • azure devops
  • Blockchain
  • Blog
  • Browser
  • C-Sharp Corner
  • C#
  • Career Advice
  • Code Snippets
  • CodeProject
  • Cognitive Services
  • Cosmos DB
  • CSS
  • CSS3
  • Data Factory
  • Database
  • Docker
  • Drawings
  • Drill Down Chart
  • English
  • Excel Programming
  • Exporting
  • Facebook
  • Fun
  • Gadgets
  • GitHub
  • GoPro
  • High Map
  • HighChart
  • How to
  • HTML
  • HTML5
  • Ignite UI
  • IIS
  • Interview
  • IoT
  • JavaScript
  • JQuery
  • jQuery UI
  • JQWidgets
  • JQX Grid
  • Json
  • Knockout JS
  • Linux
  • Machine Learning
  • Malayalam
  • Malayalam Poems
  • MDX Query
  • Microsoft
  • Microsoft ADOMD
  • Microsoft MVP
  • Microsoft Office
  • Microsoft Technologies
  • Microsoft Windows
  • Microsoft Windows Server
  • Mobile
  • MongoDB
  • Monthly Winners
  • MVC
  • MVC Grid
  • MySQL
  • News
  • Node JS
  • npm
  • Number Conversions
  • October 2015
  • Office 365
  • Office Development
  • One Plus
  • Outlook
  • Page
  • PHP
  • Poems
  • PowerShell
  • Products
  • Q&A
  • Raspberry PI
  • React
  • SEO
  • SharePoint
  • Skype
  • Social Media
  • Software
  • Spire.Doc
  • Spire.PDF
  • Spire.XLS
  • SQL
  • SQL Server
  • SSAS
  • SSMS
  • Storage In HTML5
  • Stories
  • Third Party Software Apps
  • Tips
  • Tools
  • Translator Text
  • Uncategorized
  • Unit Testing
  • UWP
  • VB.Net
  • Videos
  • Virtual Machine
  • Visual Studio
  • Visual Studio 2017
  • Wamp Server
  • Web API
  • Web Platform Installer
  • Webinars
  • WebMatrix
  • Windows 10
  • Windows 7
  • Windows 8.1
  • Wordpress
  • Writing

ABOUT ME

I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer. If you would like to know more about me, you can read my story here.

Contact Me

  • info@sibeeshpassion.com

Pages

  • About
  • Search
  • Privacy Policy
  • About
  • Search
  • Privacy Policy
© Copyright Sibeesh Passion 2014-2025. All Rights Reserved.
Go to mobile version