Log client IP address - c#

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.

Related

how to Get real IP address without using site request ? 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.

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 :)

How to get programmatically the Web browser's IP address and Port number in C#?

I am creating a Http proxy that seats between the web browser and the web server and based on my requirements the proxy server should get the IP address and port number of the web browser that has made a request. Here is a class that represent the connection between the proxy and web browser.
public class Client
{
public Client(IPAddress browserIP, int browserPort)
{
/*Use browserIP and browserPort to create a socket object*/
}
}
Note that i am not using neither HttListener nor HttpRequest objects! I have created a custom Request object that allows me to set the http headers and other stuff that the HttpRequest object doesn't do;but my Request object doesn't have a method to get the browser IP address and Port.
Check out this class
You can use the Request object to get the IP of the requesting end.
string remoteAddr = Request.UserHostAddress;
EDIT: That'll get you the Hostname. good enough to get started with!
string ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ipaddress == "" || ipaddress == null)
ipaddress = Request.ServerVariables["REMOTE_ADDR"];
Try the above. it fetches the ip of the requesting client.

Categories

Resources