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?