ASP .Net网络API 2.1获取客户端IP地址 [英] Asp Net Web API 2.1 get client IP address

查看:252
本文介绍了ASP .Net网络API 2.1获取客户端IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我需要得到客户端IP,在网络API请求的一些方法,
我试图用这个code从<一个href=\"http://www.strathweb.com/2013/05/retrieving-the-clients-ip-address-in-asp-net-web-api/\">here但它总是返回服务器的本地IP,
 如何以正确的方式获得?

  HttpContext.Current.Request.UserHostAddress;

这是其他问题:

 公共静态类的Htt prequestMessageExtensions
    {
        私人常量字符串的HttpContext =MS_HttpContext;
        私人常量字符串RemoteEndpointMessage =System.ServiceModel.Channels.RemoteEndpointMessageProperty;        公共静态字符串GetClientIpAddress(这Htt的prequestMessage要求)
        {
            如果(request.Properties.ContainsKey(HttpContext的))
            {
                动态CTX = request.Properties [HttpContext的];
                如果(CTX!= NULL)
                {
                    返回ctx.Request.UserHostAddress;
                }
            }            如果(request.Properties.ContainsKey(RemoteEndpointMessage))
            {
                动态remoteEndpoint = request.Properties [RemoteEndpointMessage]
                如果(remoteEndpoint!= NULL)
                {
                    回remoteEndpoint.Address;
                }
            }            返回null;
        }
    }


解决方案

以下链接可以帮助你。下面是从以下链接code。

引用:<一href=\"http://trikks.word$p$pss.com/2013/06/27/getting-the-client-ip-via-asp-net-web-api/\">getting-the-client-ip-via-asp-net-web-api

 使用System.Net.Http;
使用System.ServiceModel.Channels;
使用的System.Web;
使用System.Web.Http;
命名空间Trikks.Controllers.Api
{
    公共类IpController:ApiController
    {
          公共字符串GetIp()
          {
                返回GetClientIp();
          }          私人字符串GetClientIp(HTT prequestMessage要求= NULL)
          {
                请求=请求?请求;                如果(request.Properties.ContainsKey(MS_HttpContext))
                {
                      回报((HttpContextWrapper)request.Properties [MS_HttpContext])Request.UserHostAddress。
                }
                否则,如果(request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
                {
                     RemoteEndpointMessageProperty道具=(RemoteEndpointMessageProperty)request.Properties [RemoteEndpointMessageProperty.Name]
                     返回prop.Address;
                }
                否则,如果(HttpContext.Current!= NULL)
                {
                    返回HttpContext.Current.Request.UserHostAddress;
                }
                其他
                {
                      返回null;
                }
           }
     }
}

这样做的另一种方法是下面

引用:<一href=\"http://blogs.msdn.com/b/hongmeig1/archive/2012/07/09/how-to-access-the-client-s-ip-address.aspx\">how-to-access-the-client-s-ip-address

对于Web托管版

 字符串clientAddress = HttpContext.Current.Request.UserHostAddress;

对于自托管

 对象属性;
        Request.Properties.TryGetValue(typeof运算(RemoteEndpointMessageProperty).FullName,出属性);
        RemoteEndpointMessageProperty remoteProperty =财产RemoteEndpointMessageProperty;

Hello I need get client IP that request some method in web api, I have tried to use this code from here but it always returns server local IP, how to get in correct way ?

HttpContext.Current.Request.UserHostAddress;

from other questions:

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;
        }
    }

解决方案

Following link might help you. Here's code from the following link.

reference : 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;
                }
           }
     }
}

Another way of doing this is below.

reference: how-to-access-the-client-s-ip-address

For web hosted version

string clientAddress = HttpContext.Current.Request.UserHostAddress;

For self hosted

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

这篇关于ASP .Net网络API 2.1获取客户端IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆