public static string GetClientExternalIp()
{
HttpContext context = HttpContext.Current;
string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ipAddress))
{
string[] addresses = ipAddress.Split(',');
if (addresses.Length != 0)
{
return addresses[0];
}
}
return context.Request.ServerVariables["REMOTE_ADDR"];
}
I am struggling with the above code snippet, i want to get the client external IP address like what you see when browse to http://checkip.dyndns.org but above snippet returns the IP address of the Server. What i need is the IP address of the LAN the client is connecting from NOT the web server IP.
Use HttpRequest.UserHostAddress
HttpContext.Current.Request.UserHostAddress;
I use the above line of code and it returns the clients IP address.
Try this..
This works for me.
By using this method you can get the client IP address instead of Server IP.
public static string GetClientIP()
{
try
{
string VisitorsIPAddress = string.Empty;
if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
{
VisitorsIPAddr = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
}
else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
{
VisitorsIPAddress = HttpContext.Current.Request.UserHostAddress;
}
return VisitorsIPAddress;
}
catch (Exception)
{
return null;
}
}
Related
How i get public client IP in C# without external api/service
I tried use this code. But, not success.
string ipAddress = Response.HttpContext.Connection.RemoteIpAddress.ToString();
if(ipAddress == "::1")
{
ipAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList[1].ToString();
}
Console.WriteLine(ipAddress);
on ASP.NET Follow This Method:
Public string GetIp()
{
string ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ip))
{
ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
return ip;
}
otherwise use below method:
Public string GetIp() {
string IPAddress = "";
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;
}
Please up-vote if you found this helpful.
I need to get IP address of client in asp.net Controller.
I use this Code:
string ip = string.Empty;
if (request.IsSecureConnection)
ip = request.ServerVariables["REMOTE_ADDR"];
if (string.IsNullOrWhiteSpace(ip))
{
ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrWhiteSpace(ip))
{
ip = request.UserHostAddress;
}
else
{
if (ip.IndexOf(",", StringComparison.Ordinal) > 0)
{
ip = ip.Split(',').Last();
}
}
}
Sometime the ip is empty.
Do you have any idea why? Why is ip null in Httpcontext.Current.Request?
I'd like to know how to get the IP of the user connected to my application ( asp.net mvc4) .
I tried:
IPHostEntry ipHostEntry = Dns.GetHostByName(Dns.GetHostName());
IPAddress ipAddress = ipHostEntry.AddressList[0];
But it didn't work.
So how can I modify the snippet to get the Ip` or the connected user?
Here's the converted C# code from the similar question #jamieHennerley suggested.
protected string GetIPAddress()
{
System.Web.HttpContext context = System.Web.HttpContext.Current;
string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ipAddress))
{
string[] addresses = ipAddress.Split(',');
if (addresses.Length != 0)
{
return addresses[0];
}
}
return context.Request.ServerVariables["REMOTE_ADDR"];
}
I'm trying to ping a server based on an IP Address and a port, using the Ping class,
I have to convert the IP Address to an array of bytes, how am I doing it?
I took this method from somewhere
bool IsConnectedToInternet
{
get
{
Uri url = new Uri("www.abhisheksur.com");
string pingurl = string.Format("{0}", url.Host);
string host = pingurl;
bool result = false;
Ping p = new Ping();
try
{
PingReply reply = p.Send(host, 3000);
if (reply.Status == IPStatus.Success)
return true;
}
catch { }
return result;
}
}
I just have to ping the server based on an IP, not a URL.
Thank you.
Can you not just do this:
public static bool IsConnectedToInternet
{
get
{
using (var ping = new Ping())
{
try
{
var reply = ping.Send("173.194.41.168", 3000);
return reply.Status == IPStatus.Success;
}
catch
{
return false;
}
}
}
}
Although your code snippet does not require it, here is an answer to the question in the title of your post:
You can use System.Net.Dns.GetHostAddresses("www.abhisheksur.com") to get an array of IPAdresses objects representing the addresses of your host. You can then call GetAddressBytes() on an individual IPAddress object to convert it to an array of bytes.
Use the IPAddress class and Ping.Send(IPAddress address) method. If you're trying to convert from IPAddress to bytes, it offers handy hosttonetwork and networktohost methods.
http://msdn.microsoft.com/en-us/library/system.net.ipaddress.aspx
http://msdn.microsoft.com/en-us/library/hb7xxkfx.aspx
Try using the documentation for Ping.Send
How can you dynamically get the IP address of the server (PC which you want to connect to)?
System.Dns.GetHostEntry can be used to resolve a name to an IP address.
IPHostEntry Host = Dns.GetHostEntry(DNSNameString);
DoSomethingWith(Host.AddressList);
If you Use the Below Method you will be able to resolve correctly
public static bool GetResolvedConnecionIPAddress(string serverNameOrURL, out IPAddress resolvedIPAddress)
{
bool isResolved = false;
IPHostEntry hostEntry = null;
IPAddress resolvIP = null;
try
{
if (!IPAddress.TryParse(serverNameOrURL, out resolvIP))
{
hostEntry = Dns.GetHostEntry(serverNameOrURL);
if (hostEntry != null && hostEntry.AddressList != null && hostEntry.AddressList.Length > 0)
{
if (hostEntry.AddressList.Length == 1)
{
resolvIP = hostEntry.AddressList[0];
isResolved = true;
}
else
{
foreach (IPAddress var in hostEntry.AddressList)
{
if (var.AddressFamily == AddressFamily.InterNetwork)
{
resolvIP = var;
isResolved = true;
break;
}
}
}
}
}
else
{
isResolved = true;
}
}
catch (Exception ex)
{
}
finally
{
resolvedIPAddress = resolvIP;
}
return isResolved;
}
You want to do an nslookup.
Here's an example:
http://www.c-sharpcorner.com/UploadFile/DougBell/NSLookUpDB00112052005013753AM/NSLookUpDB001.aspx
Based on your comment on chaos's answer, you don't want the IP address of a server, you want the IP address of a client. If that's the case, fix your question ... and your answer would be HttpRequest.UserHostAddress.