Hiiiiiiiii
i need IP address of the current user in Asp.net,i used below code to get IP Address ,but i am getting IP Address of the User LAN,please suggest me
var ipAddress = HttpContext.Current.Request.ServerVariables["X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ipAddress))
{
ipAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
5 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
nagendra prasadPosted Nov 12, 2014, 12:51 AM
Joginder BangerPosted Nov 12, 2014, 12:44 AM
for more information about the method hit this link
http://stackoverflow.com/questions/19285957/how-to-get-public-ip-address-of-a-user-in-c-sharp
protected void GetUser_IP() { string VisitorsIPAddr = string.Empty; if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null) { VisitorsIPAddr = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); } else if (HttpContext.Current.Request.UserHostAddress.Length != 0) { VisitorsIPAddr = HttpContext.Current.Request.UserHostAddress; } uip.Text = "Your IP is: " + VisitorsIPAddr; }
nagendra prasadPosted Nov 12, 2014, 12:36 AM
nagendra prasadPosted Nov 12, 2014, 12:10 AM
Joginder BangerPosted Nov 11, 2014, 11:48 PM
try the following code
string hostName = Dns.GetHostName(); // Retrive the Name of HOST
Console.WriteLine(hostName);
// Get the IP
string myIP = Dns.GetHostByName(hostName).AddressList[0].ToString();
Console.WriteLine("My IP Address is :"+myIP);
Console.ReadKey();