Click here to Skip to main content
14,604,819 members
Rate this:
Please Sign up or sign in to vote.
See more:
I would like to get IP Address of client with help of asp.net(C#).How can get IP of a client machine in asp.net / C#.
Posted
Updated 25-Jul-13 6:10am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

Try:
Request.ServerVariables("REMOTE_ADDR")
or
Request.UserHostAddress
   
Comments
Uday P.Singh 27-Sep-11 9:11am
   
correct my 5!
shailendra from MP 27-Sep-11 9:38am
   
Thanks Uday pratap
[no name] 27-Sep-11 9:18am
   
my 5! to
Sharma Richa 1-Aug-12 9:18am
   
+5
[no name] 12-Feb-14 11:20am
   
I want to IP address of client from where my webpages are called in IFRAME
REMOTE_ADDR does not provide that IP address.
Any idea about it?
Rate this:
Please Sign up or sign in to vote.

Solution 3

Response.Write("Your IP address is: " + Request.ServerVariables["REMOTE_ADDR"].ToString() + "<br />");

Response.Write("You are browsing this site with: " + Request.ServerVariables["http_user_agent"].ToString() + "<br />");

Response.Write("The method used to call the page: " + Request.ServerVariables["request_method"].ToString() + "<br />");

Response.Write("The server's domain name: " + Request.ServerVariables["server_name"].ToString() + "<br />");

Response.Write("The server's port: " + Request.ServerVariables["server_port"].ToString() + "<br />");

Response.Write("The server's software: " + Request.ServerVariables["server_software"].ToString() + "<br />");

Response.Write("The DNS lookup of the IP address is: " + Request.ServerVariables["REMOTE_HOST"].ToString());
   
Comments
[no name] 27-Sep-11 9:19am
   
shailendra from MP only asked for client IP
Rate this:
Please Sign up or sign in to vote.

Solution 2

Get users IP address

Oct 30, 2006 07:41 AM | LINK

HttpContext.Current.Request.UserHostAddress;
or
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

To get the IP address of the machine and not the proxy use the following code

HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
   
Rate this:
Please Sign up or sign in to vote.

Solution 6

public static string GetIpAddress()  // Get IP Address
    {
        string ip = "";     
        IPHostEntry ipEntry = Dns.GetHostEntry(GetCompCode());
        IPAddress[] addr = ipEntry.AddressList;
        ip = addr[2].ToString();
        return ip;
    }
    public static string GetCompCode()  // Get Computer Name
    {   
        string strHostName = "";
        strHostName = Dns.GetHostName();
        return strHostName;
    }
   
Comments
CHill60 30-Jul-13 11:07am
   
Did you realise this question is nearly 2 years old?
HienHam 16-Oct-13 0:43am
   
Great Solution. Thanks very much
sweta shah 11-Feb-15 23:01pm
   
hi... nice post
now i have to find location from ip address.
how to find location from ip address
please help me.
Thank You
Rate this:
Please Sign up or sign in to vote.

Solution 7

protected void Button1_Click(object sender, EventArgs e)
    {
    string strHostName = System.Net.Dns.GetHostName();
    string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

    SqlCommand cmd = new SqlCommand("insert into tblIpAddress(IPAddress)values(@IPAddress)", con);
    cmd.Parameters.AddWithValue("@IPAddress", clientIPAddress);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();

    }
   
Comments
Deepu S Nair 21-Jan-15 8:57am
   
Answering old questions adds nothing to the previous solution and is likely to attract
downvoting.
CHill60 21-Jan-15 9:14am
   
And the insert to a database is completely off-topic!
Rate this:
Please Sign up or sign in to vote.

Solution 5

How to get client IP in asp.net(C#).

string strHostName = System.Net.Dns.GetHostName();
       string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(1).ToString();
   
v2
Comments
Nelek 10-Jun-13 4:17am
   
Did you realize that the question is from Sept-2011?
[no name] 25-Jul-13 4:18am
   
yes Nelek
CHill60 10-Jun-13 8:27am
   
And was already solved

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100