<?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>IP Restrictions on Net 7 &#8211; Sibeesh Passion</title>
	<atom:link href="https://sibeeshpassion.com/tag/ip-restrictions-on-net-7/feed/" rel="self" type="application/rss+xml" />
	<link>https://sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Fri, 11 Aug 2023 16:11:07 +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>IP Restrictions on Net 7 &#8211; Sibeesh Passion</title>
	<link>https://sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Async Client IP safelist for Dot NET</title>
		<link>https://sibeeshpassion.com/async-client-ip-safelist-for-dot-net/</link>
					<comments>https://sibeeshpassion.com/async-client-ip-safelist-for-dot-net/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Fri, 11 Aug 2023 13:58:43 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Action filters in dot net 7]]></category>
		<category><![CDATA[Async call on Program file]]></category>
		<category><![CDATA[Async Client IP safelist for ASP.NET Core]]></category>
		<category><![CDATA[Async Startup]]></category>
		<category><![CDATA[IP Restrictions on Net 7]]></category>
		<category><![CDATA[Net 7 Web API]]></category>
		<guid isPermaLink="false">https://sibeeshpassion.com/?p=14617</guid>

					<description><![CDATA[Here, in this post we will see, how we can get the Safe List IP addresses asynchronously and add that to configure our ActionFilterAttribute and apply the same to the Web API to make sure that the unidentified requests gets 403 Forbidden.]]></description>
										<content:encoded><![CDATA[
<p>I have a .Net Web API project and I wanted to implement a mechanism via code to make sure that only certain IP addresses are allowed to call that API. We could do this by configuring the networking rules, Virtual Network, NSG implementations or even Azure App Access Restrictions if we host the Web API in an Azure App Service. However, the reason why I wanted to implement this via code is that the IP addresses list gets updated frequently and I wanted to make sure that there is very less maintenance on the service. Here, in this post we will see, how we can get the Safe List IP addresses asynchronously and add that to configure our ActionFilterAttribute and apply the same to the Web API to make sure that the unidentified requests gets 403 Forbidden.</p>



<h2 class="wp-block-heading">Create an IP Action Filter</h2>



<p>Before you do this, I assume that you already have the .Net Web API. For this post I am using .Net 7. Create a Services folder and then create a class IpActionFilter that is inherited from ActionFilterAttribute.</p>



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



<p>The implementation is a clone of this <a href="https://learn.microsoft.com/en-us/aspnet/core/security/ip-safelist?view=aspnetcore-7.0#action-filter">doc</a>. As you can see from that doc, there it the safe IP addresses list are static and is added to the App Settings manually. However in my scenario, I had to get this from an Async service call.</p>



<h1 class="wp-block-heading">Get the IP addresses from an Asyn service call</h1>



<p>We will create a new Service IpFilterService that implements IIpFilterService. Here we will mimic the async call to make this post more concrete. </p>



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



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



<h1 class="wp-block-heading">Async call to set the configuration</h1>



<p>If you just have a few IP addresses that can easily set in the App Settings. You can just add them manually as in this <a href="https://learn.microsoft.com/en-us/aspnet/core/security/ip-safelist?view=aspnetcore-7.0#ip-address-safelist">post</a>. As I wanted to update this list via an async service call, there are a few additional changes we must do. One of them is creating a Hosted Service that implements IHostedService and add that to the builder using builder.Services.AddHostedService. Let&#8217;s create IpHostedService. </p>



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



<p>As you can see from the code above, we are setting the AdminSafeIpList configuration to our App Settings via the code. Now all we need to add is to add this to the builder. </p>



<h1 class="wp-block-heading">Configure the builder</h1>



<p>To make sure that the AdminSafeIpList is updated to the App Settings, we will need to add our IpHostedService as a HostedService. You can learn more about this service <a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-7.0&amp;tabs=visual-studio">here</a>. The code for Program.cs after this changes is below. </p>



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



<p>So we are passing the AdminSafeIpList to the IpActionFilter. </p>



<h1 class="wp-block-heading">Enable IpActionFilter as a ServiceFilter</h1>



<p>As we have implemented the action filter and other required services, we now can add the IpActionFilter as a ServiceFilter in the controller or the actions. Add the code [ServiceFilter(typeof(IpActionFilter))] to top of controller or actions. </p>



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



<h1 class="wp-block-heading">Finally, Add some unit tests</h1>



<p>Let&#8217;s try to add some unit tests for our IpActionFilter.</p>



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



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



<p>Let&#8217;s build and run our API. You should see the preceeding response. </p>



<p><strong>With External IPs:</strong></p>



<figure class="wp-block-image size-large"><a href="/wp-content/uploads/2023/08/403-Forbidden-Result.png"><img fetchpriority="high" decoding="async" width="1024" height="607" src="/wp-content/uploads/2023/08/403-Forbidden-Result-1024x607.png" alt="" class="wp-image-14619" srcset="/wp-content/uploads/2023/08/403-Forbidden-Result-1024x607.png 1024w, /wp-content/uploads/2023/08/403-Forbidden-Result-300x178.png 300w, /wp-content/uploads/2023/08/403-Forbidden-Result-768x455.png 768w, /wp-content/uploads/2023/08/403-Forbidden-Result-400x237.png 400w, /wp-content/uploads/2023/08/403-Forbidden-Result-1012x600.png 1012w, /wp-content/uploads/2023/08/403-Forbidden-Result.png 1255w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure>



<p><strong>With Internal IPs:</strong></p>



<figure class="wp-block-image size-large is-resized"><a href="/wp-content/uploads/2023/08/Without-IP-restrictions.png"><img decoding="async" src="/wp-content/uploads/2023/08/Without-IP-restrictions-839x1024.png" alt="" class="wp-image-14620" style="width:840px;height:1024px" width="840" height="1024" srcset="/wp-content/uploads/2023/08/Without-IP-restrictions-839x1024.png 839w, /wp-content/uploads/2023/08/Without-IP-restrictions-246x300.png 246w, /wp-content/uploads/2023/08/Without-IP-restrictions-768x937.png 768w, /wp-content/uploads/2023/08/Without-IP-restrictions-400x488.png 400w, /wp-content/uploads/2023/08/Without-IP-restrictions-492x600.png 492w, /wp-content/uploads/2023/08/Without-IP-restrictions.png 846w" sizes="(max-width: 840px) 100vw, 840px" /></a></figure>



<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">Conclusion</h1>



<p>Here in this post we have seen,</p>



<p>1. How we can restrict the accesse to our Web APIs by providing the Safe IP addresses list</p>



<p>2. How to create a new Action Filter and use that for Controllers</p>



<p>3. One of the challenge was to get the IP Address safe list from an Async service call and update the App Settings. </p>



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



<p>I am yet another developer who is passionate about writing and video creation. 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/async-client-ip-safelist-for-dot-net/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
