hi all,

In my page i need to get the system ip address. i used this code.

string myHost = System.Net.Dns.GetHostName();
string myIP = System.Net.Dns.GetHostByName(myHost).AddressList[0].ToString();

this is working fine when i run application in local system. Now i hosted that application in server say(). so now when i access that application from my system i am getting ipaddress as which is servers IP. But i need my system ip address.. so can anyone help me please..

Thank u.

Dani AI

Generated

Short version: your original DNS calls ran on the server, so they returned the server host/IP. The correct place to read the client's address is the incoming HTTP request. correctly discovered this using the request environment. ' Dns approach will always reflect the machine the code runs on, and 's external service idea will give the client's public IP only if the call is made from the client (or the server's public IP if called server-side). was right that client-side reporting is required if you need a private LAN address.

A robust server-side pattern is: prefer a trusted proxy header (X-Forwarded-For) when you sit behind proxies/load balancers, parse the left-most non-empty entry (it is usually the original client), and fall back to the remote IP the server actually sees. Example for classic ASP.NET:

public static string GetClientIp(System.Web.HttpRequest request)
{
    string xff = request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? request.Headers["X-Forwarded-For"];
    if (!string.IsNullOrEmpty(xff))
    {
        var ip = xff.Split(',').Select(s => s.Trim())
                    .FirstOrDefault(s => !string.IsNullOrEmpty(s) && !s.Equals("unknown", StringComparison.OrdinalIgnoreCase));
        if (!string.IsNullOrEmpty(ip)) return ip;
    }
    return request.UserHostAddress;
}

For ASP.NET Core the equivalent is to read HttpContext.Connection.RemoteIpAddress and enable the Forwarded Headers middleware when behind proxies so that RemoteIpAddress reflects the original client.

Troubleshooting and cautions: X-Forwarded-For can be spoofed, so only trust it from known reverse-proxies/load-balancers. If you need the client's private LAN IP (192.168.x.x) the server cannot discover it unless the client voluntarily sends it (client-side JS or a form). For security-sensitive use, validate the obtained IP against trusted proxy lists and be explicit about whether you want public vs. private addresses.

Recommended Answers

All 7 Replies

Try this. I don't understand the problem fully but this is what I use to get the computer's 'external' IP that you can connect to it from, especially if it is behind a router.

string IP = new WebClient().DownloadString("");

Let me explain you clearly..
i developed a website. In that when i open it will automatically take IP address of my system in a textbox. Now i hosted that in a server through IIS. When we access that application in different systems we are getting same IP address i;e IP address of the server where i hosted. But i need to get the IP address of the system in which i opened the application.

than you need to write that to client side.. i mean using java script or something like that..

and pass it to server..

it's just a theory..

try this

string myHost = System.Net.Dns.GetHostName();
Label1.Text = myHost;

string myIP = System.Net.Dns.GetHostByName(myHost).AddressList[0].ToString();
Label2.Text = myIP;

i tried that but i am not getting clients IP address

After hours of search in google my problem got solved here is what i had done

string myIP = this.Page.Request.ServerVariables["REMOTE_ADDR"];

if you run this in local we get "127.0.0.1" but when it is hosted it gives the clients IP address.

internet protocol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.