Click here to Skip to main content
15,910,872 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I want to get ip Address of user who loggend into my website. Can anybody plz help me to do so.
I'm using code but it is returning ::1
Here is my code:

C#
public static string getclientIP()
  {
      string result = string.Empty;
      string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
      if (!string.IsNullOrEmpty(ip))
      {
          string[] ipRange = ip.Split(',');
          int le = ipRange.Length - 1;
          result = ipRange[0];
      }
      else
      {
          result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
      }

      return result;
  }
Posted
Updated 25-Jan-20 8:11am
Comments
Dipali_Wagh 8-Jul-13 5:10am    
its not working,
this is giving ip address as- ::1

Configure your IP with "inetmgr"
 
Share this answer
 
C#
rivate string GetUserIP()
 {
     return Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? Request.ServerVariables["REMOTE_ADDR"];
 }


Thanks & Regard
Sham
 
Share this answer
 
Comments
Dipali_Wagh 6-Jul-13 4:05am    
as i have written my code. this is giving ip address as- ::1
its not working
You Just Try this code



use this dll
using System.Net;

C#
public string GetIP()
    {
        string strHostName = "";
        strHostName = System.Net.Dns.GetHostName();

        IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);

        IPAddress[] addr = ipEntry.AddressList;

        return addr[addr.Length-1].ToString();

    }
 
Share this answer
 
v2
Comments
Dipali_Wagh 6-Jul-13 4:07am    
its working
thanx
but tell me will it return ip address of client or not?
means it should be different not ip address of server
Dipali_Wagh 6-Jul-13 9:04am    
hey one problem occured because of this.
if i change my network then it gets changed.
arrayList gets changed.
so plz sujjest me something to get exact array index
Dipali_Wagh 8-Jul-13 5:10am    
hey plz reply me
string clientIp = (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? Request.ServerVariables["REMOTE_ADDR"]).Split(',')[0].Trim();

this might help u....
 
Share this answer
 
Comments
Dipali_Wagh 8-Jul-13 1:34am    
Hi, this solution is also not working. It is also giving ip address as ::1
manjunathmk 8-Jul-13 5:37am    
this will give when u use browse your project using localhost like htttp://localhost/loginpage.aspx try using the ip address of local machine it will give correct else u publish it on server and test it.

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