How to get the Client IP address in asp.net?


Hi

Here is the simple code to do this task

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();

}

Advertisement

5 thoughts on “How to get the Client IP address in asp.net?

  1. sakthi March 23, 2011 / 9:45 am

    Hi,

    Here its work localhost but it host in server the ip address show blank

  2. Chandra Dev March 23, 2011 / 11:23 am

    Hi
    Please check it properly.I have used this code in one live project.It was working fine.

  3. trupti December 19, 2012 / 7:32 pm

    This wil give server ip address and not visitor /client ip address

    • Chandra Dev December 20, 2012 / 8:13 am

      Thanks for updating me. I will check it.

  4. cara cantik June 3, 2014 / 5:59 pm

    I really like reading an article that can make people think.
    Also, thank you for permitting me to comment!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.