BrMe
BrMe

Reputation: 315

How to get LAN IP of a client using .net?

I want to get the adrress IP (LAN ip) of the computer which access my site.

How can i get it?

Upvotes: 1

Views: 3612

Answers (4)

BrMe
BrMe

Reputation: 315

Use this function:

 public string GetLanIP()
    {
        IPHostEntry host;
        string localIP = "?";
        host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (IPAddress ip in host.AddressList)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                localIP = ip.ToString();
            }
        }
        return localIP;

    }

Upvotes: 0

Umur Kontacı
Umur Kontacı

Reputation: 35478

You can't.

Browsers don't send their local IPs in HTTP headers, so there is no way for you to get it. You only get the router's external (internet) IP.

Upvotes: 1

johan
johan

Reputation: 6666

I assume you're using asp.net and in that case you could use Request.UserHostAddress to retrieve the IP address of a client.

Upvotes: 0

Sampath
Sampath

Reputation: 65870

Method 1:

You can get that by using below mentioned link.

List the IP Address of all computers connected to a single LAN

Method 2:

You can try below one also.

public string GetUserIP()
    {
        string ipList = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (!string.IsNullOrEmpty(ipList))
        {
            return ipList.Split(',')[0];
        }

        return Request.ServerVariables["REMOTE_ADDR"];
    }

Method 3:

System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList.GetValue(0).ToString();

I hope this will help to you.

Upvotes: 0

Related Questions