Bhavin Bhaskaran
Bhavin Bhaskaran

Reputation: 582

How to get the client ip address in asp.net mvc?

Hi I am using a payment gateway.To which I have to pass the ip address of the client ( someone using my website) . How to get it? I tried to use the following code when I run my project in local system

System.Net.Dns.GetHostByName(Environment.MachineName).AddressList[0].ToString();

and i got some value. But when I checked with https://www.whatismyip.com/ I got some other value. So which is correct? How can I get the correct value? I am using asp.net MVC. is there any change in this code if we are using mvc other than asp.net?

Upvotes: 2

Views: 734

Answers (2)

Tech Savant
Tech Savant

Reputation: 3766

You are getting your private/local IP address assigned by your router when you do it locally, the website you mentioned is showing you the public/external IP address that is seen by the public.

As for correcting it, you don't need to. When a client is connected, ASP will return the correct public/external IP.

Private vs Public IP

Upvotes: 2

Royi Namir
Royi Namir

Reputation: 148514

Your local IP within your network is :

System.Net.Dns.GetHostByName(Environment.MachineName).AddressList[0].ToString().Dump();

But you can be behind a firewall / router so/your organization must have unique IP which is measured by WhatsMyIp.com

Upvotes: 2

Related Questions