DotNet获取客户端的IP地址
image
public static string GetClientIpAddress(HttpRequestMessage request){ if (request.Properties.ContainsKey(HttpContext)) { dynamic ctx = request.Properties[HttpContext]; if (ctx != null) { return ctx.Request.UserHostAddress; } } if (request.Properties.ContainsKey(RemoteEndpointMessage)) { dynamic remoteEndpoint = request.Properties[RemoteEndpointMessage]; if (remoteEndpoint != null) { return remoteEndpoint.Address; } } if (request.Properties.ContainsKey(OwinContext)) { dynamic ctx = request.Properties[OwinContext]; if (ctx != null) { return ctx.Request.RemoteIpAddress; } } return null; }
作者:CoderMiner
链接:https://www.jianshu.com/p/d193558f52ad