如何在WebMethod中获取调用者的IP地址?

时间:2021-01-09 07:09:16

How do I get the caller's IP address in a WebMethod?

如何在WebMethod中获取调用者的IP地址?

[WebMethod]
public void Foo()
{
    // HttpRequest... ? - Not giving me any options through intellisense...
}

using C# and ASP.NET

使用c#和ASP.NET

6 个解决方案

#1


81  

HttpContext.Current.Request.UserHostAddress is what you want.

HttpContext.Current.Request。UserHostAddress是您想要的。

#2


8  

Just a caution. IP addresses can't be used to uniquely identify clients. NAT Firewalls and corporate proxies are everywhere, and hide many users behind a single IP.

只是一个警告。IP地址不能用于唯一地标识客户端。NAT防火墙和公司代理随处可见,并将许多用户隐藏在一个IP后面。

#3


6  

Try:

试一试:

Context.Request.UserHostAddress

#4


4  

Try this:

试试这个:

string ipAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

Haven't tried it in a webMethod, but I use it in standard HttpRequests

还没有在webMethod中尝试过,但是我在标准的httprequest中使用过

#5


2  

The HttpContext is actually available inside the WebService base class, so just use Context.Request (or HttpContext.Current which also points to the current context) to get access to the members provided by the HttpRequest.

HttpContext实际上在WebService基类中是可用的,所以只需使用Context。请求(或HttpContext。Current(也指向当前上下文)访问HttpRequest提供的成员。

#6


0  

I made the following function:

我做了如下的功能:

static public string sGetIP()
{
    try
    {
        string functionReturnValue = null;

        String oRequestHttp =
            WebOperationContext.Current.IncomingRequest.Headers["User-Host-Address"];
        if (string.IsNullOrEmpty(oRequestHttp))
        {
            OperationContext context = OperationContext.Current;
            MessageProperties prop = context.IncomingMessageProperties;
            RemoteEndpointMessageProperty endpoint =
                prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
            oRequestHttp = endpoint.Address;
        }
        return functionReturnValue;
    }
    catch (Exception ex)
        {
            return "unknown IP";
        }
}

This work only in Intranet, if you have some Proxy or natting you should study if the original IP is moved somewhere else in the http packet.

这只在内部网中工作,如果您有代理或natting,您应该研究原始IP是否移动到http包的其他位置。

#1


81  

HttpContext.Current.Request.UserHostAddress is what you want.

HttpContext.Current.Request。UserHostAddress是您想要的。

#2


8  

Just a caution. IP addresses can't be used to uniquely identify clients. NAT Firewalls and corporate proxies are everywhere, and hide many users behind a single IP.

只是一个警告。IP地址不能用于唯一地标识客户端。NAT防火墙和公司代理随处可见,并将许多用户隐藏在一个IP后面。

#3


6  

Try:

试一试:

Context.Request.UserHostAddress

#4


4  

Try this:

试试这个:

string ipAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

Haven't tried it in a webMethod, but I use it in standard HttpRequests

还没有在webMethod中尝试过,但是我在标准的httprequest中使用过

#5


2  

The HttpContext is actually available inside the WebService base class, so just use Context.Request (or HttpContext.Current which also points to the current context) to get access to the members provided by the HttpRequest.

HttpContext实际上在WebService基类中是可用的,所以只需使用Context。请求(或HttpContext。Current(也指向当前上下文)访问HttpRequest提供的成员。

#6


0  

I made the following function:

我做了如下的功能:

static public string sGetIP()
{
    try
    {
        string functionReturnValue = null;

        String oRequestHttp =
            WebOperationContext.Current.IncomingRequest.Headers["User-Host-Address"];
        if (string.IsNullOrEmpty(oRequestHttp))
        {
            OperationContext context = OperationContext.Current;
            MessageProperties prop = context.IncomingMessageProperties;
            RemoteEndpointMessageProperty endpoint =
                prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
            oRequestHttp = endpoint.Address;
        }
        return functionReturnValue;
    }
    catch (Exception ex)
        {
            return "unknown IP";
        }
}

This work only in Intranet, if you have some Proxy or natting you should study if the original IP is moved somewhere else in the http packet.

这只在内部网中工作,如果您有代理或natting,您应该研究原始IP是否移动到http包的其他位置。