在Web API身份验证过滤器中捕获请求IP地址 [英] Capture request IP Address in Web API Authentication Filter
本文介绍了在Web API身份验证过滤器中捕获请求IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想捕获调用我的Web API服务的客户端的IP地址.我试图在我创建的自定义身份验证过滤器中捕获该IP地址.
I would like to capture the IP Address of the client that calls my Web API service. I am trying to capture that IP address in a custom Authentication Filter that I have created.
请求IP地址是否可以从 HttpActionContext
获得?
Is the request IP address available from the HttpActionContext
?
我似乎找不到它.
身份验证过滤器在捕获发出请求的客户端IP地址的位置错误吗?
Is the Authentication Filter the wrong place where to capture the IP address of the client making the request ?
推荐答案
我最近找到了以下扩展方法:
I recently found the following extension method for that:
public static string GetClientIpAddress(this HttpRequestMessage request)
{
if (request.Properties.ContainsKey("MS_HttpContext"))
{
return IPAddress.Parse(((HttpContextBase)request.Properties["MS_HttpContext"]).Request.UserHostAddress).ToString();
}
if (request.Properties.ContainsKey("MS_OwinContext"))
{
return IPAddress.Parse(((OwinContext)request.Properties["MS_OwinContext"]).Request.RemoteIpAddress).ToString();
}
return null;
}
您现在可以致电:
HttpActionContext.Request.GetClientIpAddress();
这篇关于在Web API身份验证过滤器中捕获请求IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文