29

I have been trying to figure this out but cannot find a reliable way to get a clients IP address when making a request to a page in asp.net that works with all servers.

  • This question has been asked before. Check these links out:- - [How to get user client ip address in asp.net?](https://stackoverflow.com/questions/735350/how-to-get-user-client-ip-address-in-asp-net) - [Browser IP Address](https://stackoverflow.com/questions/756067/browser-ip-address) HTH. (vote to close this). – Pure.Krome May 15 '09 at 04:36

8 Answers8

38

One method is to use Request object:

protected void Page_Load(object sender, EventArgs e)
{
    lbl1.Text = Request.UserHostAddress;
}
TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
8
 IpAddress=HttpContext.Current.Request.UserHostAddress;
Taran
  • 2,895
  • 25
  • 22
5
Request.ServerVariables["REMOTE_ADDR"]

To access an index or property on C#, you should use [ ] instead of ( )

Community
  • 1
  • 1
Jason
  • 86,222
  • 15
  • 131
  • 146
0

Use this code:

public static string GetIpAddress()
    {
        return HttpContext.Current != null ? HttpContext.Current.Request.UserHostAddress : "";
    }
  • Please explain your answer a little better rather than just putting in code. Try to explain why you answered this way. – Difster Aug 01 '17 at 10:36
  • HttpContext.Current.Request.UserHostAddress this contains our Ip address for this first of all we have to check HttpContext.Current is null or not – Ankur vijay Aug 03 '17 at 12:21
  • use this code in code behind of Asp.net public static string GetIpAddress() { return HttpContext.Current != null ? HttpContext.Current.Request.UserHostAddress : ""; } – Ankur vijay Oct 12 '17 at 09:44
0
System.Web.HttpContext.Current.Request.UserHostAddress;
Elnaz
  • 2,854
  • 3
  • 29
  • 41
  • You might want to explain why this answer is better than the ones that were already given (in 2009 and 2015) – Hans Kesting Nov 01 '17 at 07:55
  • @HansKesting it's full reference. It's not an answer for questioner, But for anyone else needs it today – Elnaz Nov 01 '17 at 13:24
0

Try this code:

var IpAddress = Request.Headers["Referer"][0];
Bùi Đức Khánh
  • 3,975
  • 6
  • 27
  • 43
  • Please consider adding a bit of description to your question or perhaps a link to the documentation for this. This will help this answer be more complete and helpful further into the future. – Derek C. Jul 16 '20 at 17:15
0

If there are proxies between client and server. HTTP_X_FORWARDED_FOR header can be used.

var ips = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
var clientIp = "";
if (!string.IsNullOrEmpty(ips))
{
    string[] addresses = ips.Split(',');
    if (addresses.Length != 0)
    {
        clientIp = addresses[0];
    }
}
else
{
    clientIp = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
0

You can use HttpContext with property bellow:

var _request1 = HttpContext.Current.Request.UserHostAddress;
        string requestedDomain = HttpContext.Current.Request.ServerVariables["HTTP_HOST"];
        string requestScheme = HttpContext.Current.Request.Url.Scheme;
        string requestQueryString = HttpContext.Current.Request.ServerVariables["QUERY_STRING"];
        string requestUrl = HttpContext.Current.Request.ServerVariables["URL"];
Apple Yellow
  • 307
  • 1
  • 2
  • 7