1

I'm currently using MVC4 web API and for one of the calls I need to get the client's IP address. Here is my setup...

Client computers have no internet access, only access to our intranet

since I am making cross domain calls, I have to iframe a web server page into the client HTML page and post a message.

Once the web server receives the message, it makes an ajax call to the RESTful service and C# takes it from there...

So my current goal is the get the IP address of the client machine. After some research, I found this...

private string GetClientIp(HttpRequestMessage request)
{
    if (request.Properties.ContainsKey("MS_HttpContext"))
    {
      return ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;
    }
    else if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
    {
        RemoteEndpointMessageProperty prop;
        prop =  (RemoteEndpointMessageProperty)this.Request.Properties[RemoteEndpointMessageProperty.Name];
        return prop.Address;
    }
   else
   {
    return null;
   }

Now I realize I'm not the smartest person, but what would I pass for the parameter of HTTPRequestMessage request?

private string GetClientIp(HttpRequestMessage request)

Is there an example of this? Is this possible? Or is there another approach I should take for this

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
prawn
  • 2,643
  • 2
  • 33
  • 49
  • 1
    In general, if a method expects an argument of type `HttpRequestMessage` then you declare that and pass it in. – Jeremy Thompson Sep 11 '13 at 23:32
  • checkout this question: [How to get a user's client IP address in ASP.NET?](http://stackoverflow.com/questions/735350/how-to-get-a-users-client-ip-address-in-asp-net) – shawnzhu Sep 12 '13 at 00:40

1 Answers1

1

Excuse the VB.NET but you could do this also:

Add to your Global.asax (this will include the current session in your Web Api Controller):

    Private Shared Function IsWebApiRequest() As Boolean
        Return HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath.StartsWith("~/api")
    End Function

 Protected Sub Application_PostAuthorizeRequest()
        If IsWebApiRequest() Then
            HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required)
        End If
    End Sub

Then in your Web API Controller:

Public Shared Function GetIPAddress() As String
    Dim context As System.Web.HttpContext =
        System.Web.HttpContext.Current
    Dim sIPAddress As String =
        context.Request.ServerVariables("HTTP_X_FORWARDED_FOR")
    If String.IsNullOrEmpty(sIPAddress) Then
        Return context.Request.ServerVariables("REMOTE_ADDR")
    Else
        Dim ipArray As String() = sIPAddress.Split(
            New [Char]() {","c})
        Return ipArray(0)
    End If
End Function

You will be breaking RESTful design with this but it is all about what works, people are too dogmatic about REST in regards to certain types of projects in my opinion anyways.

Brian Ogden
  • 18,439
  • 10
  • 97
  • 176