0
public string GetIp()
{
    string ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (string.IsNullOrEmpty(ip))
    {
        ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    }
    return ip;
}

This is what i tried this code is not working.

haldo
  • 11,588
  • 5
  • 34
  • 42
  • Are you looking for `Request.Headers.TryGetValue`? – OneCricketeer Dec 24 '21 at 04:59
  • I closed this as a duplicate of https://stackoverflow.com/questions/1907195/how-to-get-ip-address - though I know the OP posted the exact same code as the accepted answer, there are other answers that provide alternatives that you should try first. – Dai Dec 24 '21 at 05:01
  • 2
    "this code is not working" is not an adequate description of the problem. You should only repost your question after you've explained **why** the code doesn't work for you, as well as a dump of your `HttpRequest` collections (including `ServerVariables`). – Dai Dec 24 '21 at 05:01
  • @OneCricketeer `Request.Headers` isn't going to contain `REMOTE_ADDR` (or other equivalents). for that use `HttpRequest.UserHostAddress` – Dai Dec 24 '21 at 05:03

0 Answers0