2

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?
1

2 Answers 2

4

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;  
         }     
     }  
  }  
}  
Sign up to request clarification or add additional context in comments.

2 Comments

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?
Yes you can use (userip == "127.0.0.1" || userip == "::1" ) like this , till date I didn't find that predefined .Net function
2

Try this property:

System.Web.HttpContext.Current.Request.UserHostAddress

And you could use your own implemented ActionFilterAttribute to filter requests

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.