I need to block one IP address or class in asp.net
Can anyone help me with the code? And how to implement?
Thanks
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.
An IP whitelist can be implemented in ASP.NET Core by using middleware or by using MVC action filters.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With