0

Using the bellow code .

protected string GetUserIP()
{
    string strUserIP = string.Empty;
    if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
    {
        strUserIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
    }
    else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
    {
        strUserIP = HttpContext.Current.Request.UserHostAddress;
    }
    return strUserIP;
}

i get the IPaddress like the format ::1 .

How do I get the correct IP address of a system.

Amarnath Balasubramanian
  • 9,300
  • 8
  • 34
  • 62
LiS
  • 39
  • 7

6 Answers6

1

It is of localhost ::1 if you use on web server you will get the correct one.

Though it will depend on the configuration of the network from where the user is accessing your application.

There can be firewall which doesn't expose the actual IP of the client system.

शेखर
  • 17,412
  • 13
  • 61
  • 117
1

Method that gets IP address: ASP.NET, C#

using System;
using System.Web;

namespace WebApplication1
{
    public class Global : HttpApplication
    {
      protected void Application_BeginRequest(object sender, EventArgs e)
      {
        // Get request.
        HttpRequest request = base.Request;

        // Get UserHostAddress property.
        string address = request.UserHostAddress;

        // Write to response.
        base.Response.Write(address);

        // Done.
        base.CompleteRequest();
      }
    }
}
Venkatesh K
  • 4,364
  • 4
  • 18
  • 26
0

To get the IpAddress use the below code

string IPAddress = GetIPAddress();
    public string GetIPAddress()
    {

        IPHostEntry Host = default(IPHostEntry);
        string Hostname = null;
        Hostname = System.Environment.MachineName;
        Host = Dns.GetHostEntry(Hostname);
        foreach (IPAddress IP in Host.AddressList) {
            if (IP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) {
                IPAddress = Convert.ToString(IP);
            }
        }
        return IPAddress;

    }

Source

Community
  • 1
  • 1
Amarnath Balasubramanian
  • 9,300
  • 8
  • 34
  • 62
0

Client IP can be read from request:

context.Request.ServerVariables["REMOTE_HOST"] 

Here is code for getting more than just client IP address:

string browserInfo =
             "RemoteUser=" + context.Request.ServerVariables["REMOTE_USER"] + ";\n"
            + "RemoteHost=" + context.Request.ServerVariables["REMOTE_HOST"] + ";\n"
            + "Type=" + context.Request.Browser.Type + ";\n"
            + "Name=" + context.Request.Browser.Browser + ";\n"
            + "Version=" + context.Request.Browser.Version + ";\n"
            + "MajorVersion=" + context.Request.Browser.MajorVersion + ";\n"
            + "MinorVersion=" + context.Request.Browser.MinorVersion + ";\n"
            + "Platform=" + context.Request.Browser.Platform + ";\n"
            + "SupportsCookies=" + context.Request.Browser.Cookies + ";\n"
            + "SupportsJavaScript=" + context.Request.Browser.EcmaScriptVersion.ToString() + ";\n"
            + "SupportsActiveXControls=" + context.Request.Browser.ActiveXControls + ";\n"
            + "SupportsJavaScriptVersion=" + context.Request.Browser["JavaScriptVersion"] + "\n";

(or)

string IPAddress = string.Empty;
string SearchName = string.Empty;

String strHostName = HttpContext.Current.Request.UserHostAddress.ToString();

IPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

ServerVariables

Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115
0

You can find IP Address in a way given below. It works for me :) If you want only one IP then pick up first IP from the list.

var Dnshost = Dns.GetHostEntry(Dns.GetHostName());

string ipAddress = "";

IPAddress[] ipAddress = Dnshost.AddressList;
ipAddress = ipAddress[0].ToString();

Here "ipAddress[0]" will give you current IP of system. And if you want all IP's to get then iterate the AddressList as shown below:

foreach (var ip in Dnshost.AddressList)
 {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                ipAddress = ip.ToString();
            }
 }

NOTE: It will give you the IPv4 Addresses in case of "AddressFamily.InterNetwork" and if you need IPv6 Addresses you will use "AddressFamily.InterNetworkV6".

Hope it will be helpful for you.

Zia Ul Mustafa
  • 301
  • 3
  • 14
0

::1 is the IPv6 localhost address, you can find more information here

Óscar Andreu
  • 1,630
  • 13
  • 32