how to Get real IP address without using site request ? c# - c#

as you can see I am using below code to get any real ip address on any PC connected to the internet but I was wondering if there is way to get the real IP address without calling a site to get it ?
and if there is a way to send it to my hotmail/gmail/...etc account as email
i found many ways on the internet but all of them are blocked by mail servers
private string getExternalIp()
{
try
{
string externalIP;
externalIP = (new WebClient()).DownloadString("http://checkip.dyndns.org/");
externalIP = (new Regex(#"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"))
.Matches(externalIP)[0].ToString();
return externalIP;
}
catch
{
MessageBox.Show("Please Call Technical Support", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}

I don't think there is any reliable method to get external IP address without actually connecting to something. This is because your machine may be behind NAT and simply doesn't have information what is it's external IP address.
Of course if you know this machine has configured it's external IP address on one of it's interfaces, you may list all interfaces using NetworkInterface.GetAllNetworkInterfaces.

Related

C# - TcpClient Timeout on IMAP Hostname that doesnt exist

So I have a very basic script:
#region Get Hostname IP
IPAddress[] dnsRecords = Dns.GetHostAddresses(hostname);
string ipHostname = dnsRecords[0].ToString();
if (ipHostname.Length == 0) {
responseCode = imapLoginResponse.Failed;
return false;
}
#endregion
#region Try make a request to the Host, Port
try {
_Connection = new TcpClient(ipHostname, port);
} catch (SocketException ex) {
System.Diagnostics.Debug.Write("-" + hostname + "-" + ipHostname + "- " + ex.Message);
responseCode = ex.Message == "No such host is known" || ex.Message.StartsWith("No connection could be made because the target machine actively refused it") ? imapLoginResponse.BadHostname : imapLoginResponse.Failed;
return false;
}
The code above simply tries to connect to an IMAP Server.
Get imap.{domain.ext} DNS Record IP
Try connect to that IP with port 993
Voila
What's happening for either dead/bad/abandoned/parked websites is:
Get imap.{domain.ext} DNS Record IP, somehow will get one even though it doesn't exist
Try to connect to that IP with port 993
Server doesn't respond resulting in a timeout
Now what do I do? I cant turn this down to an "BadHostname" as it could just be a connection issue on the user's end or even not connected to internet.
If I were to just retry here, it will end up infinitely looping.
While yes, I could do a 5 retries == invalid thing, but that's not accurate and could still be a temporary internet issue.
My question is, why is it getting a DNS Record IP for a record that doesnt exist?
And what am I meant to do when something like this occurs?
If you need to test, try it with this: imap.celerityinc.com (no celebrityinc.com) and you can see for yourself.
Name resolution and services provided on an IP address are independent.
You get a DNS record because it does exist at some point in the DNS hierarchy with a certain lifetime.
What you can do when something like this occurs? Flush your local DNS cache. Try different DNS servers. Go back to the SOA with your own DNS query solution.

Log client IP address

I am trying to log the client IP address for one of the applications that I have build. It works OK on the dev server. But as soon as we deploy the solution on to the production server, which has load balancer,it seems to log the Load Balancer's IP address rather than the client IP.
I understand that we need to add HTTP_X_FORWARD_FOR header.
This is my code :
private static string GetCurrentIp()
{
String clientIP =
(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] == null) ?
HttpContext.Current.Request.UserHostAddress :
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
return clientIP;
}
Appreciate if someone could help me identify the mistake that I have done
Sometimes your visitors are behind either a proxy server or a router and the standard Request.UserHostAddress only captures the IP address of the proxy server or router. When this is the case the user's IP address is then stored in the server variable ("HTTP_X_FORWARDED_FOR").
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"];
}
above code helps you to make it working.
Another viable option is:
var remoteIpAddress = request.HttpContext.Connection.RemoteIpAddress;
Not saying you've got to use this, but the process is much simpler for you with this method.
Previously this was broken in .NET Core RC1 but turns out it's production ready since RC2.

Get User's IP Address in a Share Point Web Part not giving valid IP address

This code in a standard custom WebPart:
private string GetUserIP()
{
return Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? Request.ServerVariables["REMOTE_ADDR"];
}
is returning: "fe80::c564:7922:d873:5cf5%11" instead of a valid IP address. It does this for every method I've found on Google for retrieving it, including HttpRequest.UserHostAddress.
Does anyone have an idea what is going on?
Edit: For some reason it's giving me IPv6 when loading the page locally, but works as intended when I access from a different machine :/
Use this,
string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ip))
{
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
Or use
string ipAddress = Request.UserHostAddress

getting strange ip when using Request.servervariables c#

My web application runs on a local IIS server. When calling the api of my web app
using fiddler i get a strange client ip adress.
public static class HttpRequestMessageHelper
{
public static string GetClientIpAddress(this HttpRequestMessage request)
{
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"].ToString();
if (string.IsNullOrEmpty(ip))
{
return "Unknown IP-Adress";
}
}
return ip;
}
}
I am using this extension method for obtaining the ip.
The ip i am getting looks like this: "fe80::745a:d3fa:db2c:7b94%11"
That's an IPv6 address:
http://en.wikipedia.org/wiki/IPv6
As the world is running of IPv4 addresses (xxx.xxx.xxx.xxx) the new standard is to bring in a lot more addresses.
Sadly they're not as easy to remember, so DNS lookups will be vital IMHO.
You can check you adaptor settings / ipconfig and / or your DHCP server (local router perhaps?) and see, chances are you being issued IPv6 addresses.

C# Get Proxy IP like PHP

Got a Game Emulator built in C#.
When a user logs into it, it records their IP address for banning etc...
We are behind a reverse proxy, so of course all connections coming into the emulator are the IP of the proxy.
But on our PHP page for register, we used this code...
function getRealIpAddr() {
if (!empty($_SERVER['HTTP_CLIENT_IP']))
//check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
//to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
And that manages to return the proper IP of the client through our reverse proxy.
This is how I am at the moment getting the IP of the client in C#...
public string RemoteAddress
{
get
{
return ((mSocket != null && mSocket.Connected ?
mSocket.RemoteEndPoint.ToString().Split(':')[0] : string.Empty));
}
}
To give you some context, mSocket is the Socket of the client connecting
Is there a function for C# that can do the same? All the answers I have seen direct me to ASP but I don't need that!
I have decided to when the user logs into it using PHP, it will now set their IP instead of the emulator doing it when they connect to that!
Cheers
Richard :)

Categories

Resources