Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a web api in asp.net mvc. On Post request, i want to get ip address of client's/users's machine. I have tried following code,but getting the ip address of server. Please help

What I have tried:

HttpContext.Current.Request.UserHostAddress;



string ip5 = HttpContext.Current.Request.Params["HTTP_CLIENT_IP"] ?? HttpContext.Current.Request.UserHostAddress;




ip7 = HttpContext.Current.Request.UrlReferrer.ToString();



string ip10 = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
              if (ip10 != null || ip10 != String.Empty)
              {
                  ip10 = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
              }
Posted
Updated 14-Sep-20 4:20am

Hello:
I´m using
HttpContext.Current.Request.UserHostAddress;
in my application and it works ok.
Perhaps in the code where you process the post there are more places where you assign the ip of the machine of the client. A breakpoint and exam the value of HttpContext.Current.Request.UserHostAddress could help you.
 
Share this answer
 
This is a very frequently asked question and if you googled it before posting you would have found the answer that you can't get the IP of the client machine, only the machine the request came from (so a proxy server for example). If the code you are using is returning the server IP then there is something about your set-up you're not telling us such as the server acting as a proxy to the clients.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900