1

I have an ASP.NET MVC application (using .NET 4.5) deployed on a web server which has IIS 8.5 installed.

I have created a custom controller class where I do some stuff and it inherits from System.Web.Mvc.Controller:

public partial class MyCustomController : System.Web.Mvc.Controller
{
    // Here my stuff
}

Then, all my controllers (except a few ones), inherit from my custom controller, for example:

public partial class OneController : MyCustomController
{
   // Here some stuff
}

My goals:

  1. Now, I need to get the client IP address that is currently making the request to my ASP.NET MVC application. So I would like to implement a method within my custom controller, MyCustomController, that returns that client IP. Is this possible at this point? If so how?
  2. Additionally, how can I know if the incoming request comes from local IP address (localhost) 127.0.0.1 and then if so, discard this request, I mean, do nothing?
Willy
  • 9,848
  • 22
  • 141
  • 284
  • 1
    You could try if this works, it seems to have been made using .NET Framework MVC: [MVC - Block IP Address Using Action Filter](https://www.c-sharpcorner.com/article/mvc-block-ip-address-using-action-filter/) – Peter B Nov 10 '20 at 09:24

2 Answers2

3

You can use HttpRequest.ServerVariables to get the IP address of a client in ASP.NET MVC. The REMOTE_ADDR variable gives the IP address of the client.

You can directly use the below method to your controller page and call it from your view or wherever you need it

   public string GetIp()  
   {  
      string ip = 
      System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];  
      if (string.IsNullOrEmpty(ip))  
      {  
        ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];  
      }  
    return ip;  
   }  

The second method of getting an IP address is using the built-in functionality of ASP.NET. Here we use the Request property of the Page class, which gets an object of HttpRequest class for the requested page. The HttpRequest is a sealed class that enables ASP.NET to read the HTTP values sent by the client browser during a Web request. We access the UserHostAddress property of the HttpRequest class to get the IP Address of the visitor.

    private void GetIpAddress(out string userip)  
    {  
      userip = Request.UserHostAddress;  
      if (Request.UserHostAddress != null)  
     {  
       Int64 macinfo = new Int64();  
       string macSrc = macinfo.ToString("X");  
       if (macSrc == "0")  
       {  
        if (userip == "127.0.0.1")  
        {  
            Response.Write("visited Localhost!");  
        }  
        else  
        {  
            lblIPAdd.Text = userip;  
         }     
     }  
  }  
}  
Red
  • 3,030
  • 3
  • 22
  • 39
Dhrumil shah
  • 611
  • 4
  • 23
  • Could it be userip equals to "::1" in case of IPv6 IP address? so the conditional would be userip == "127.0.0.1" || userip == "::1" Also is there some predefined .Net function that passing a string IP it says if it is local or not? – Willy Nov 13 '20 at 01:07
  • Yes you can use (userip == "127.0.0.1" || userip == "::1" ) like this , till date I didn't find that predefined .Net function – Dhrumil shah Dec 10 '20 at 10:58
1

Try this property:

System.Web.HttpContext.Current.Request.UserHostAddress

And you could use your own implemented ActionFilterAttribute to filter requests

MoRRt
  • 191
  • 1
  • 7