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

2
  • 1
    In general, if a method expects an argument of type HttpRequestMessage then you declare that and pass it in. Commented Sep 11, 2013 at 23:32
  • checkout this question: How to get a user's client IP address in ASP.NET? Commented Sep 12, 2013 at 0:40

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

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.