Skip to content

Instantly share code, notes, and snippets.

@MikeJansen
Created May 10, 2012 14:36
Show Gist options
  • Select an option

  • Save MikeJansen/2653453 to your computer and use it in GitHub Desktop.

Select an option

Save MikeJansen/2653453 to your computer and use it in GitHub Desktop.

Revisions

  1. Mike Jansen revised this gist May 10, 2012. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion HttpRequestMessageHelper.cs
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    // Original idea: http://stackoverflow.com/questions/9565889/get-the-ip-address-of-the-remote-host

    using System.Net.Http;
    using System.ServiceModel.Channels;
    using System.Web;
    @@ -24,4 +26,4 @@ public static string GetClientIpAddress(this HttpRequestMessage request)
    }
    }
    }
    }
    }
  2. Mike Jansen revised this gist May 10, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion HttpRequestMessageHelper.cs
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    using System.ServiceModel.Channels;
    using System.Web;

    namespace InsureCommon.Api
    namespace CrowSoftware.Api
    {
    public static class HttpRequestMessageHelper
    {
  3. Mike Jansen created this gist May 10, 2012.
    27 changes: 27 additions & 0 deletions HttpRequestMessageHelper.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    using System.Net.Http;
    using System.ServiceModel.Channels;
    using System.Web;

    namespace InsureCommon.Api
    {
    public static class HttpRequestMessageHelper
    {
    public static string GetClientIpAddress(this 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)request.Properties[RemoteEndpointMessageProperty.Name];
    return prop.Address;
    }
    else
    {
    return null;
    }
    }
    }
    }