首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Asp Net Web API 2.1获取客户端IP地址

Asp Net Web API 2.1获取客户端IP地址
EN

Stack Overflow用户
提问于 2014-03-20 20:19:08
回答 9查看 151.5K关注 0票数 124

您好,我需要获取客户端IP,这是请求在web api中的一些方法,我已经尝试使用此代码从here,但它总是返回服务器本地IP,如何获得正确的方式?

HttpContext.Current.Request.UserHostAddress;

从其他问题中:

public static class HttpRequestMessageExtensions
    {
        private const string HttpContext = "MS_HttpContext";
        private const string RemoteEndpointMessage = "System.ServiceModel.Channels.RemoteEndpointMessageProperty";

        public static string GetClientIpAddress(this HttpRequestMessage request)
        {
            if (request.Properties.ContainsKey(HttpContext))
            {
                dynamic ctx = request.Properties[HttpContext];
                if (ctx != null)
                {
                    return ctx.Request.UserHostAddress;
                }
            }

            if (request.Properties.ContainsKey(RemoteEndpointMessage))
            {
                dynamic remoteEndpoint = request.Properties[RemoteEndpointMessage];
                if (remoteEndpoint != null)
                {
                    return remoteEndpoint.Address;
                }
            }

            return null;
        }
    }
EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2014-03-20 20:23:27

下面的链接可能会对你有所帮助。下面是来自以下链接的代码。

参考:getting-the-client-ip-via-asp-net-web-api

using System.Net.Http;
using System.ServiceModel.Channels;
using System.Web;
using System.Web.Http;


namespace Trikks.Controllers.Api
{
    public class IpController : ApiController
    {
          public string GetIp()
          {
                return GetClientIp();
          }

          private string GetClientIp(HttpRequestMessage request = null)
          {
                request = request ?? Request;

                if (request.Properties.ContainsKey("MS_HttpContext"))
                {
                      return   ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;
                }
                else if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
                {
                     RemoteEndpointMessageProperty prop = (RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name];
                     return prop.Address;
                }
                else if (HttpContext.Current != null)
                {
                    return HttpContext.Current.Request.UserHostAddress;
                }
                else
                {
                      return null;
                }
           }
     }
}

下面是另一种方法。

参考:how-to-access-the-client-s-ip-address

适用于web托管版本

string clientAddress = HttpContext.Current.Request.UserHostAddress;

对于自托管

object property;
        Request.Properties.TryGetValue(typeof(RemoteEndpointMessageProperty).FullName, out property);
        RemoteEndpointMessageProperty remoteProperty = property as RemoteEndpointMessageProperty;
票数 137
EN

Stack Overflow用户

发布于 2014-08-08 02:16:13

使用Web API 2.2:Request.GetOwinContext().Request.RemoteIpAddress

票数 75
EN

Stack Overflow用户

发布于 2014-03-21 01:48:25

尝试使用以下命令获取Ip地址

ip = HttpContext.Current != null ? HttpContext.Current.Request.UserHostAddress : "";
票数 20
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22532806

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档