user2211918
user2211918

Reputation: 91

How to block specified IP address programmatically in c#

I want to block the IP address for a specified range. I have written the code in the web.config file as:

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true"/>
  <security>
    <ipSecurity allowUnlisted="false"></ipSecurity>
  </security>
</system.webServer>

Which will block everything.

But when I run this code nothing getting blocked.

Upvotes: 2

Views: 4481

Answers (2)

Grant Castner
Grant Castner

Reputation: 1

This might help. I use it on a site where I don't have access to IIS. It works successfully in blocking individual IP addresses. It probably could be modified to block ranges.

http://www.hanselman.com/blog/AnIPAddressBlockingHttpModuleForASPNETIn9Minutes.aspx

Upvotes: 0

ᗩИᎠЯƎᗩ
ᗩИᎠЯƎᗩ

Reputation: 2132

I suggest you to take a look at the answer to the following question, maybe it can help you:

"I doubt you can do that at the web.config level unless the "IP and Domain Restrictions" role is installed on the server itself."

Web.config 500 block ipaddress - Internal server error

Even if you're not getting the 500 error, maybe you just have to add the:

<clear/>

to remove all upstream restrictions? Let me know.

EDIT: added instructions to configure Restriction access to websites using IP address in IIS 7.

Here's a good link, explaining how to install "IP and Domains restrictions" in IIS 7, as this feature is not installed by default. If you don't install this feature, restrictions won't work!

http://linuxwebservertips.in/restrict-access-to-website-using-ip-address-in-iis-7/

Follow this guide and everything will work fine, As you can see, you can add multiple range restrictions too.

Upvotes: 2

Related Questions