1
我使用Owin.Testing作为测试环境。在我的控制器中,我需要从调用者获取远程IP地址。使用Owin获取远程ip。测试
//in my controller method
var ip = GetIp(Request);
的Util
private string GetIp(HttpRequestMessage request)
{
return request.Properties.ContainsKey("MS_HttpContext")
? (request.Properties["MS_HttpContext"] as HttpContextWrapper)?.Request?.UserHostAddress
: request.GetOwinContext()?.Request?.RemoteIpAddress;
}
结果属性不包含OwinContext的MS_HttpContext和RemoteIpAddress为空。
是否有任何选项可以获取IP?