I am having some issues getting the full IP address with C#. When I call a C# web method via a button click, it displays (or should) the IP address in a JavaScript alert box. What I am getting instead of the IP address is ::1. I am running this via Visual Studio 2015 Community. Here is my code:
[WebMethod]
public string getIPAddress()
{
// this method gets the ip address by using the server variables
// in C# to capture the data from the client
HttpContext context = HttpContext.Current;
string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ipAddress))
{
string[] addresses = ipAddress.Split(',');
if (addresses.Length != 0)
{
return addresses[0];
}
}
return context.Request.ServerVariables["REMOTE_ADDR"];
}
And the method that handles the button click:
// display the ip address to the user if the button is clicked (display it via a javascript alert box)
public void IpAddress(object sender, EventArgs e)
{
Data d = new Data();
Response.Write("<script type=\"text/javascript\">alert('" + d.getIPAddress() + "');</script>");
}
Any help would be appreciated. Thanks!