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
-
Answer:
You can get the IP address of the client using the http://msdn.microsoft.com/en-us/library/system.web.httprequest.userhostaddress%28v=VS.100%29.aspx (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 http://msdn.microsoft.com/en-us/library/system.web.httpapplication.beginrequest.aspx of the application (typically in Global.asax). If you detect a blocked address, you can output an empty (placeholder) page using http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.transfer.aspx (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).
FinalDestiny at Stack Overflow Visit the source
Other answers
If what you mean by "block" is "don't let them harass my server", this is not an asp.net issue, 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
egrunin
you mention you are not familiarized with the ASP.NET, so, maybe this excelent article from http://www.west-wind.com can help you as it as a full article on how to block IP's and even have an admin area to manage them... http://www.west-wind.com/WebLog/posts/59731.aspx
balexandre
Related Q & A:
- How To Change IP Address?Best solution by Super User
- How can I allow user to create posts in website using ASP.NET?Best solution by Programmers
- How to Undo previous action in asp.net?Best solution by forums.asp.net
- How do you block your IP address when emailing people?Best solution by Yahoo! Answers
- How to fool an IP address Tracer into believing another IP address?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.