星空网 > 软件开发 > ASP.net

.NET 客户IP地址捕捉

MVC模式下要获取客户IP可以在ActionFilterAttribute中进行拦截

1 filterContext.HttpContext.Request.UserHostAddress

同样,在WebAPI中也可以用同样的方式获取,只是继承自System.Web.Http.Filters.ActionFilterAttribute

 1 private string GetClientIP(HttpActionContext actionContext) 2     { 3       if (actionContext.Request.Properties.ContainsKey("MS_HttpContext")) 4       { 5         return ((HttpContextWrapper)actionContext.Request.Properties["MS_HttpContext"]).Request.UserHostAddress; 6       } 7  8       if (actionContext.Request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name)) 9       {10         RemoteEndpointMessageProperty prop;11         prop = (RemoteEndpointMessageProperty)actionContext.Request.Properties[RemoteEndpointMessageProperty.Name];12         return prop.Address;13       }14 15       return null;16     }

 




原标题:.NET 客户IP地址捕捉

关键词:.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流