webAPI获得链接客户端IP地址
2016-06-12 11:22
323 查看
public static class HttpRequestMessageExtensions { private const string HttpContext = "MS_HttpContext"; private const string RemoteEndpointMessage = "System.ServiceModel.Channels.RemoteEndpointMessageProperty"; private const string OwinContext = "MS_OwinContext"; public static string GetClientIpAddress(this HttpRequestMessage request) { // Web-hosting. Needs reference to System.Web.dll if (request.Properties.ContainsKey(HttpContext)) { dynamic ctx = request.Properties[HttpContext]; if (ctx != null) { return ctx.Request.UserHostAddress; } } // Self-hosting. Needs reference to System.ServiceModel.dll. if (request.Properties.ContainsKey(RemoteEndpointMessage)) { dynamic remoteEndpoint = request.Properties[RemoteEndpointMessage]; if (remoteEndpoint != null) { return remoteEndpoint.Address; } } // Self-hosting using Owin. Needs reference to Microsoft.Owin.dll. if (request.Properties.ContainsKey(OwinContext)) { dynamic owinContext = request.Properties[OwinContext]; if (owinContext != null) { return owinContext.Request.RemoteIpAddress; } } return null; } }
相关文章推荐
- iOS开发-自定义专属相册 (详细)| 干货
- Ubuntu 14 环境下编译安装 Redis 并将其注册为系统服务
- Android基础UI组件
- 启动tomcat 服务报 The file is absent or does not have execute permission
- Android jni 资源引用报错
- 使用Java如何让需求在指定时间内生效
- Ubuntu 14 环境下编译安装 Redis 并将其注册为系统服务
- 专题四1003
- 为什么我们要使用新型Web安全协议HSTS? - 站长之家
- linux node&& npm 安装方式
- logic:iterator struts1 功能小例 (转)
- 存储过程重新编译一直卡死
- rss订阅
- 计算重要天文坐标系之间的转换(C#)
- 机器学习--线性回归
- Tomcat The JRE_HOME environment variable is not defined correctly
- Hibernate实体映射
- Java框架SSH结合Easyui控件实现省市县三级联动示例解析
- HashMap类
- html css相对单位