Click here to Skip to main content
15,920,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
what code right in asp.net with C# to find the ip address of client's computer?
Posted

C#
using System;
using System.Web;

namespace WebApplication4
{
    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();
    }
    }
}
 
Share this answer
 
v2
Try this one:

C#
public string getclientIPAddress()
{
   string result = string.Empty;
   string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (!string.IsNullOrEmpty(ip))
            {
                string[] ipRange = ip.Split(',');
                int le = ipRange.Length - 1;
                result = ipRange[0];
            }
            else
            {
                result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }

            return result;
        }



Please mark as answer and vote 5 if this solved your problem

Regards,
Eduard
 
Share this answer
 
Comments
Dhanashree Dive 4-Apr-13 2:31am    
I m getting output as :
2002:7643:f87b::7643:f87b
I want out in format as example 123.16.76.01
Sharda Vishwakarma 24-Feb-14 6:51am    
I'm also geting same out output but I want a perfect Local IP of client
This is Context.Request.UserHostAddress, where Context has the type System.Web.HttpContext and can be found as System.Web.HttpContext.Current or you can accesse it via the Context property of your page System.Web.UI.Page, as it is shown in the code sample in the first of MDSN help pages references below:
http://msdn.microsoft.com/en-us/library/system.web.httpcontext%28v=VS.100%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.web.ui.page.aspx[^].

Keep in mind that in most cases it will be dynamic IP address which does not uniquely identify the client computer.

—SA
 
Share this answer
 
v2
Its a quite common question in CP so its better to have a search before asking question. whatever have a look there-[Ip address and ASp.net ][^] having similar question with plenty of solutions.
 
Share this answer
 
I am using this code to find the ip address and stored into table ' ipaddress_details '
C#
protected void ip_addr()
   {
       string sIPAddress = null;
       sIPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
       if (string.IsNullOrEmpty(sIPAddress))
           sIPAddress = Request.ServerVariables["REMOTE_ADDR"];

       SqlConnection con = new SqlConnection(conn);
       try
       {
           con.Open();
           string str = "insert into ipaddress_details(ipaddr,date_time,username) values('" + Convert.ToString(sIPAddress) + "','" + Convert.ToString(DateTime.Now) + "','" + txt_usr.Text.ToString().Trim() + "')";
           SqlCommand cmd = new SqlCommand(str, con);
           cmd.ExecuteNonQuery();
       }
       finally
       {
           con.Close();
       }
   }
 
Share this answer
 
v2
 
Share this answer
 
try this
For LAN
C#
string sHostName = System.Net.Dns.GetHostName();
 System.Net.IPHostEntry ipE =System.Net.Dns.GetHostEntry(sHostName);
    System.Net.IPAddress[] IpA = ipE.AddressList; 

for Ip address use IpA[0].ToString()
for Hostname use sHostName
for DNS
try
C#
Request.ServerVariables["REMOTE_HOST"] 
 
Share this answer
 
v2
you want pc ipaddress try this it's work in c#
C#
using System;
using System.Net;

public class IPNetworking
{
  public static string GetIP4Address()
  {
    string IP4Address = String.Empty;

    foreach (IPAddress IPA in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
    {
      if (IPA.AddressFamily.ToString() == "InterNetwork")
      {
        IP4Address = IPA.ToString();
        break;
      }
    }

    if (IP4Address != String.Empty)
    {
      return IP4Address;
    }

    foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
    {
      if (IPA.AddressFamily.ToString() == "InterNetwork")
      {
        IP4Address = IPA.ToString();
        break;
      }
    }

    return IP4Address;
  }
}
 
Share this answer
 
CSS
I had the same issue . its got resolved with this url .. i hope this will help you too..

http://dotnetstock.com/technical/how-to-get-ip-address-of-a-client-system-using-asp-net
 
Share this answer
 
WELL ..
TRY THIS IF I WORKS OUT
VB
<%
Response.Write(Request.ServerVariables("REMOTE_ADD R"))
%>
 
Share this answer
 

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