Click here to Skip to main content
15,906,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
string strHostName = System.Net.Dns.GetHostName();
         objUsers.ClientIP = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();



i am using above code to get the client IP address of the when it is host. when i am testing in local system it working fine i am getting IP:198.168.1.143. The problem arise when it is host I am getting IP address as: fe80::ed72:d42c:27a3:e8df%10

How to get the client IP in proper manner Can any one help me.....
Posted

 
Share this answer
 
 
Share this answer
 
Try This....
C#
public static string GetIPAddress(HttpRequestBase request)
{
string ip = string.Empty;
try
{
ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ip))
{
if (ip.IndexOf(",") > 0)
{
string[] allIps = ip.Split(',');
int le = allIps.Length - 1;
ip = allIps[le];
}
}
else
{
ip = request.UserHostAddress;
}

HttpContext.Current.Session["IpAddress"] = ip;
}
catch (Exception ex)
{
throw ex;
}

return ip;
}
 
Share this answer
 
v2
Hi, Use below link, i have used this

Get Client IP Address in SQL Server[^]

This is my article and I have used this and it is very useful. :-)
 
Share this answer
 
try using this code....


C#
lblIPAddress.Text = Request.ServerVariables["REMOTE_HOST"].ToString();
 
Share this answer
 
C#
string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (ip == null)
            {
                ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

            }
            return ip;


try this...
 
Share this answer
 
v2
write this method to get IP address

C#
public string getipaddress()
    {
        string ipaddress;

        ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (ipaddress == "" || ipaddress == null)

            ipaddress = Request.ServerVariables["REMOTE_ADDR"];

        return ipaddress;
    }
 
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