欢迎使用WordPress的。这是您的第一篇文章。编辑或删除它,然后开始写作吧!
public static class HttpRequestMessageExtensions { private const string HttpContext = "MS_HttpContext"; private const string RemoteEndpointMessage = "System.ServiceModel.Channels.RemoteEndpointMessageProperty"; private const string OwinContext = "MS_OwinContext"; public static string GetClientIpAddress(this HttpRequestMessage request) { //Web-hosting if (request.Properties.ContainsKey(HttpContext)) { dynamic ctx = request.Properties[HttpContext]; if (ctx != null) { return ctx.Request.UserHostAddress; } } //Self-hosting if (request.Properties.ContainsKey(RemoteEndpointMessage)) { dynamic remoteEndpoint = request.Properties[RemoteEndpointMessage]; if (remoteEndpoint != null) { return remoteEndpoint.Address; } } //Owin-hosting if (request.Properties.ContainsKey(OwinContext)) { dynamic ctx = request.Properties[OwinContext]; if (ctx != null) { return ctx.Request.RemoteIpAddress; } } return null; } }