首页 > Web开发 > 详细

.NET 客户IP地址捕捉

时间:2016-11-18 11:57:23      阅读:249      评论:0      收藏:0      [点我收藏+]

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地址捕捉

原文:http://www.cnblogs.com/zc-bao/p/customerIP_get.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!