skip to Main Content

I need to block one IP address or country range IP in asp.net website

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

2

Answers


  1. There’s an example of how you could achieve this in the documentation on the Mircosoft site.
    https://learn.microsoft.com/en-us/aspnet/core/security/ip-safelist?view=aspnetcore-5.0

    This should give you an idea on how to approach your issue and rework it to suit your own needs.

    Login or Signup to reply.
  2. on the global.asax on Application_BeginRequest you can check the ip range and if its on your range to block, then stop the processing there (or redirect them to some other page)

    protected void Application_BeginRequest(Object sender, EventArgs e)
    {
        // check this -> 
        HttpContext.Current.Request.UserHostAddress with your limits
        // and then 
        
        if(doNotAllow)
        {
            HttpContext.Current.Response.TrySkipIisCustomErrors = true;
            HttpContext.Current.Response.StatusCode = 403;
            HttpContext.Current.Response.End();
            return ;     
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search