Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to block IP address or IP classes in ASP.NET

I need to block one IP address or class in asp.net

Can anyone help me with the code? And how to implement?

Thanks

like image 559
FinalDestiny Avatar asked May 23 '10 20:05

FinalDestiny


People also ask

How do I block an IP address on a web server?

Head to the “Security” section and find the “IP Address Deny Manager”, then enter a specific IP address or range of addresses to block. Here, the result is more substantive: Anyone trying to access your site from these addresses will get an error message instead of seeing your page.

How do I whitelist an IP address in net core?

An IP whitelist can be implemented in ASP.NET Core by using middleware or by using MVC action filters.


1 Answers

You can get the IP address of the client using the HttpRequest.UserHostAddress property (an instance can be accessed using this.Request from any page or using static property HttpContext.Current).

As far as I know, there is no standard method that would compare the IP address with a specified range, so you'll need to implement this bit yourself.

You'll probably want to check this for every request, which can be done either in the OnInit method of every page (that you want to block) or in the BeginRequest event of the application (typically in Global.asax).

If you detect a blocked address, you can output an empty (placeholder) page using Server.Transfer method (Response.End would be another alternative, but that simply cuts the page - returning an empty page, while Server.Transfer allows you to output some message to the client).

like image 123
Tomas Petricek Avatar answered Nov 15 '22 05:11

Tomas Petricek