.NET 8 New and Efficient Way to Check IP is in Given IP Range
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
3. Select checkbox Use preview of the .NET SDK (requires restart)
4. Make sure to restart after enabling this
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
.
The Contains method retruns true if the given IPAddress is part of the IP network. Otherwise, returns false.
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,
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.