0

I am trying to get IP V4 of client's machine in asp.net, I tried below piece of code:

Request.ServerVariables["REMOTE_ADDR"]; Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; Request.UserHostAddress;

But these all giving me IP V6 of client machine, How can I get IP V4 ? Any help or suggestion will be appreciated.

1
  • Disable IPV6 on your server if you don't want to or can't support it. Commented Dec 17, 2018 at 2:33

2 Answers 2

0

You can use the following code to show client IPAddress V4:

string strHostName = System.Net.Dns.GetHostName();
        string clientIPAddress = System.Net.Dns.GetHostAddresses
            (strHostName).GetValue(1).ToString();

Maybe the client has many Network config, so there can be more than one IP v4 address. You can change number "1" in the code to get which one you like.

Update:

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but it did not work and its still giving IP V6.
@ChupChapCharli Did you try to change the number inside GetValue function? app a breakpoint in this line then you can see several IP. select the one you need.
Its giving me IP address of server not of client.
I see. @ChupChapCharli, as the second solution you can use javascript to get client IP and then pass it to the controller or database or ... try this link : ourcodeworld.com/articles/read/257/…
0

I have used this API: https://ipstack.com/ and they give IP and other details very accurate but they are paid for SSL protected sites.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.