views:

1998

answers:

5

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

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

using C# and ASP.NET

+10  A: 

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

Darren Kopp
A: 

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.

troethom
A: 

Try this:

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

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

Slace
+1  A: 

Try:

Context.Request.UserHostAddress
Kev
+2  A: 

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.

davenpcj