zoukankan      html  css  js  c++  java
  • webAPI获得链接客户端IP地址

     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;
            }
        }
    慎于行,敏于思!GGGGGG
  • 相关阅读:
    sql 生成随机数 以及不重复随机数
    值提供器 AND 模型绑定器
    web项目在iis配置好后不能正确访问问题集锦,以及IIS常规设置
    js
    性能优化
    MVC3;0问题与知识点
    EntityFramework
    MSSQL2008 临时总结文档2014
    py--使用__slots__
    py--使用@property
  • 原文地址:https://www.cnblogs.com/GarsonZhang/p/5576979.html
Copyright © 2011-2022 走看看