<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>DOTNET8 &#8211; Sibeesh Passion</title>
	<atom:link href="https://sibeeshpassion.com/tag/dotnet8/feed/" rel="self" type="application/rss+xml" />
	<link>https://sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Fri, 13 Oct 2023 09:57:53 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>DOTNET8 &#8211; Sibeesh Passion</title>
	<link>https://sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>.NET 8 New and Efficient Way to Check IP is in Given IP Range</title>
		<link>https://sibeeshpassion.com/net-8-new-and-efficient-way-to-check-ip-is-in-given-ip-range/</link>
					<comments>https://sibeeshpassion.com/net-8-new-and-efficient-way-to-check-ip-is-in-given-ip-range/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Fri, 13 Oct 2023 09:54:45 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[.NET8]]></category>
		<category><![CDATA[DOTNET8]]></category>
		<category><![CDATA[How to check IP is in IP Ranges]]></category>
		<category><![CDATA[IP in IP Ranges]]></category>
		<category><![CDATA[IPNetwork Class DOTNET8]]></category>
		<guid isPermaLink="false">https://sibeeshpassion.com/?p=14633</guid>

					<description><![CDATA[The .NET 8 has introduced a new type IPNetwork with a Contains method. It retruns true if the given IPAddress is part of the IP network. Otherwise, returns false. Here in this post, we will see how we can implement the same.]]></description>
										<content:encoded><![CDATA[
<p>In our <a href="https://sibeeshpassion.com/async-client-ip-safelist-for-dot-net/" data-type="link" data-id="https://sibeeshpassion.com/async-client-ip-safelist-for-dot-net/">last post</a> 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&#8217;s begin. </p>



<h1 class="wp-block-heading">Source Code</h1>



<p>You can also see the codes in&nbsp;<a href="https://github.com/SibeeshVenu/DotNetIpFilter/" target="_blank" rel="noreferrer noopener">this repository</a>.</p>



<h1 class="wp-block-heading">Enablling .NET8 for Your Application</h1>



<p>As you might have already guessed, the first step is to download the .NET8 from <a href="https://dotnet.microsoft.com/en-us/download/dotnet/8.0" data-type="link" data-id="https://dotnet.microsoft.com/en-us/download/dotnet/8.0">here</a>. Let&#8217;s now change the TargetFramework in our both <code>DotNetIpFilter.csproj</code> and <code>DotNetIpFilter.Test.csproj</code> files. You can get the application code from the repositroy mentioned above. </p>



<pre class="wp-block-code"><code>&lt;TargetFramework&gt;net8.0&lt;/TargetFramework&gt;</code></pre>



<p>You will also need to update your <code>Microsoft.AspNetCore.OpenApi</code> package to .NET8.</p>



<h2 class="wp-block-heading">Enable Preview .NET 8 in Visual Studio 2022</h2>



<p>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. </p>



<p>1. Update your Visual Studio 2022</p>



<p>2. Go to Tools -&gt; Manage Preview Features</p>


<div class="wp-block-image">
<figure class="aligncenter size-large is-resized"><a href="/wp-content/uploads/2023/10/image.png"><img fetchpriority="high" decoding="async" width="696" height="1024" src="/wp-content/uploads/2023/10/image-696x1024.png" alt="" class="wp-image-14636" style="aspect-ratio:0.6796875;width:336px;height:auto" srcset="/wp-content/uploads/2023/10/image-696x1024.png 696w, /wp-content/uploads/2023/10/image-204x300.png 204w, /wp-content/uploads/2023/10/image-768x1130.png 768w, /wp-content/uploads/2023/10/image-400x588.png 400w, /wp-content/uploads/2023/10/image-408x600.png 408w, /wp-content/uploads/2023/10/image.png 1038w" sizes="(max-width: 696px) 100vw, 696px" /></a><figcaption class="wp-element-caption">.NET 8 Manage Preview Features</figcaption></figure>
</div>


<p>3. Select checkbox <code>Use preview of the .NET SDK (requires restart)</code></p>



<p>4. Make sure to restart after enabling this</p>


<div class="wp-block-image">
<figure class="aligncenter size-large is-resized"><a href="/wp-content/uploads/2023/10/image-1.png"><img decoding="async" width="1024" height="655" src="/wp-content/uploads/2023/10/image-1-1024x655.png" alt="" class="wp-image-14637" style="aspect-ratio:1.5633587786259542;width:550px;height:auto" srcset="/wp-content/uploads/2023/10/image-1-1024x655.png 1024w, /wp-content/uploads/2023/10/image-1-300x192.png 300w, /wp-content/uploads/2023/10/image-1-768x491.png 768w, /wp-content/uploads/2023/10/image-1-1536x982.png 1536w, /wp-content/uploads/2023/10/image-1-2048x1310.png 2048w, /wp-content/uploads/2023/10/image-1-400x256.png 400w, /wp-content/uploads/2023/10/image-1-938x600.png 938w" sizes="(max-width: 1024px) 100vw, 1024px" /></a><figcaption class="wp-element-caption">Use .NET Previews of the .NET SDK</figcaption></figure>
</div>


<h1 class="wp-block-heading">Use .NET8 Contains Method from <code>IPNetwork</code></h1>



<p>Let&#8217;s change our IpActionFilter.cs file code as below, to use the Contains method from .NET8 IPNetwork class. </p>



<script src="https://gist.github.com/SibeeshVenu/3700843ab923f85e192b0bded299327d.js"></script>



<p>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&nbsp;<a href="https://github.com/dotnet/aspnetcore/blob/main/src/Middleware/HttpOverrides/src/IPNetwork.cs">here</a>. You can learn more about this IPNetwork.Contains(IPAddress) method&nbsp;<a href="https://learn.microsoft.com/en-us/dotnet/api/system.net.ipnetwork.contains?view=net-8.0#system-net-ipnetwork-contains(system-net-ipaddress)">here</a>. Make sure to select the version .NET 8 for the doc. If you can&#8217;t use .Net 8, you can implement your own class by taking the code from&nbsp;<a href="https://github.com/dotnet/aspnetcore/blob/main/src/Middleware/HttpOverrides/src/IPNetwork.cs">here</a>.</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>I also have a detailed StackOverflow answer <a href="https://stackoverflow.com/a/76928408/5550507" data-type="link" data-id="https://stackoverflow.com/a/76928408/5550507">here</a> on this topic.</p>
<cite>StackOverflow answer</cite></blockquote>



<h1 class="wp-block-heading">Test IP Filtering with Ranges</h1>



<p>Now, it is time to test our new implementation. Go to the <code>IpFilterService.cs</code> file and then edit the <code>GetAdminSafeIpList</code> 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. </p>



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


<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="/wp-content/uploads/2023/10/image-5.png"><img decoding="async" width="1024" height="548" src="/wp-content/uploads/2023/10/image-5-1024x548.png" alt="" class="wp-image-14641" srcset="/wp-content/uploads/2023/10/image-5-1024x548.png 1024w, /wp-content/uploads/2023/10/image-5-300x161.png 300w, /wp-content/uploads/2023/10/image-5-768x411.png 768w, /wp-content/uploads/2023/10/image-5-1536x822.png 1536w, /wp-content/uploads/2023/10/image-5-400x214.png 400w, /wp-content/uploads/2023/10/image-5-1121x600.png 1121w, /wp-content/uploads/2023/10/image-5.png 2000w" sizes="(max-width: 1024px) 100vw, 1024px" /></a><figcaption class="wp-element-caption">Compare IP Addresses and Ranges</figcaption></figure>
</div>


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


<div class="wp-block-image">
<figure class="aligncenter size-large is-resized"><a href="/wp-content/uploads/2023/10/image-3.png"><img decoding="async" width="1024" height="206" src="/wp-content/uploads/2023/10/image-3-1024x206.png" alt="" class="wp-image-14639" style="aspect-ratio:4.970873786407767;width:825px;height:auto" srcset="/wp-content/uploads/2023/10/image-3-1024x206.png 1024w, /wp-content/uploads/2023/10/image-3-300x60.png 300w, /wp-content/uploads/2023/10/image-3-768x155.png 768w, /wp-content/uploads/2023/10/image-3-1536x310.png 1536w, /wp-content/uploads/2023/10/image-3-400x81.png 400w, /wp-content/uploads/2023/10/image-3.png 1861w" sizes="(max-width: 1024px) 100vw, 1024px" /></a><figcaption class="wp-element-caption">Returns false when IP Address is not in Ranges</figcaption></figure>
</div>


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



<script src="https://gist.github.com/SibeeshVenu/cd88ae1dcc383a6f1596e4a8eef270da.js"></script>



<h1 class="wp-block-heading">Conclusion</h1>



<p>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 <a href="https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-8" data-type="link" data-id="https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-8">here</a>. </p>



<h1 class="wp-block-heading">About the Author</h1>



<p>I am yet another developer who is passionate about writing and sharing knowledge. I have written more than 500 blogs on my&nbsp;<a rel="noreferrer noopener" href="https://sibeeshpassion.com/" target="_blank">blog</a>. If you like this content, consider following me here,</p>



<ul class="wp-block-list">
<li><a href="https://github.com/SibeeshVenu">GitHub</a></li>



<li><a href="https://medium.com/@sibeeshvenu">medium</a></li>



<li><a href="https://twitter.com/sibeeshvenu">Twitter</a></li>
</ul>



<h1 class="wp-block-heading">Your turn. What do you think?</h1>



<p>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.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sibeeshpassion.com/net-8-new-and-efficient-way-to-check-ip-is-in-given-ip-range/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
