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

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

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

thanks

+6
security
source share
3 answers

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

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

You will probably want to check this for every request that can be executed either in the OnInit method of each page (which you want to block) or in the BeginRequest application event (usually in Global.asax ).

If you find a blocked address, you can display a blank (fill-in) page using the Server.Transfer method ( Response.End will be another alternative, but it just shortens the page - returns a blank page, and Server.Transfer allows Server.Transfer to display some message to the client).

+4
source share

If you mean "block", it is "do not let them bother my server", this is not an asp.net problem, you need a firewall (software or hardware).

If what you mean by โ€œblockโ€ is โ€œdon't show my pagesโ€:

 ' pseudocode, I haven't checked the exact syntax Sub Page_Load() If HttpRequest.UserHostAddress = "123.123.123.1" then Response.Redirect "404.htm" ' send them elsewhere end if End Sub 
+4
source share

You mentioned that you are not familiar with ASP.NET, so perhaps this excellent article from Rick can help you, as a complete article on how to block IP and even have an administrative area to manage them ...

http://www.west-wind.com/WebLog/posts/59731.aspx

0
source share

All Articles