Home > Software engineering >  how to block country range ip in ASP.net website
how to block country range ip in ASP.net website

Time:09-30

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?

CodePudding user response:

There's an example of how you could achieve this in the documentation on the Mircosoft site. https://docs.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.

CodePudding user response:

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 ;     
    }
}
  • Related