Click here to Skip to main content
15,880,956 members
Please Sign up or sign in to vote.
1.17/5 (5 votes)
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

Try:
C#
Request.ServerVariables("REMOTE_ADDR")
or
C#
Request.UserHostAddress
 
Share this answer
 
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?
XML
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());
 
Share this answer
 
Comments
[no name] 27-Sep-11 9:19am    
shailendra from MP only asked for client IP
VB
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"];
 
Share this answer
 
HTML
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;
    }
 
Share this answer
 
Comments
CHill60 30-Jul-13 11:07am    
Did you realise this question is nearly 2 years old?
thanhtt90 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
C#
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();

    }
 
Share this answer
 
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!
How to get client IP in asp.net(C#).

C#
string strHostName = System.Net.Dns.GetHostName();
       string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(1).ToString();
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900