Skip to content

Instantly share code, notes, and snippets.

@lski
Created July 31, 2014 10:43
Show Gist options
  • Save lski/52744090e73e46bbbc3b to your computer and use it in GitHub Desktop.
Save lski/52744090e73e46bbbc3b to your computer and use it in GitHub Desktop.
Returns the Clients Ip Address from the Request message
using System.Web;
namespace System.Net.Http {
public static class HttpRequestMessageExt {
/// <summary>
/// Returns the Clients Ip Address from the Request message
///
/// Note: Use owin context version Request.GetOwinContext().Request.RemoteIpAddress (owin context can also be retrieved from (OwinContext)request.Properties["MS_OwinContext"]
/// </summary>
public static string GetClientIp(this HttpRequestMessage request) {
return (request.Properties.ContainsKey("MS_HttpContext") ? ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress : (string)null);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment