getting strange ip when using Request.servervariables c# - 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.

Related

Get client IP in Azure through MVC 5 controller

I can't find a question or post specific to Azure for this, and I'm not sure what's different in the environment in Azure versus my testing environments that would cause this.
I've tried a few methods to get this to work but I'm not coming right. Please note this isn't Webapi, so using the HttpRequestMessage, as far as I know, is not going to work either.
Here's what I've tried so far:
Method 1:
string ipAddress = "";
IPHostEntry Host = default(IPHostEntry);
Host = Dns.GetHostEntry(System.Environment.MachineName);
ipAddress = Host.AddressList.SingleOrDefault(x => x.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).MapToIPv4().ToString();
Method 2:
string userIP = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(userIP))
{
userIP = Request.ServerVariables["REMOTE_ADDR"];
}
return userIP;
Thanks in advance.
Read the value transported in this header:
x-forwarded-for: "91.23.44.24:52623"
Note there's a source port number trailing the IP address, so parse accordingly.
Also, as #NicoD correctly points out, the header in question may contain an array of proxy servers the request traversed. For example:
x-forwarded-for: "91.23.44.24:52623, 91.23.44.155"
Syntax
X-Forwarded-For: <client>, <proxy1>, <proxy2>
<client> The client IP address
<proxy1>, <proxy2> If a request goes through multiple proxies, the IP addresses of each successive proxy is listed. This means, the right-most IP address is the IP address of the most recent proxy and the left-most IP address is the IP address of the originating client.
What about that glaring port number you ask?
From https://www.rfc-editor.org/rfc/rfc7239:
5.2. Forwarded For
[...] this parameter MAY instead contain an IP
address (and, potentially, a port number).
All client requests are terminated in the frontend layer and proxied via Application Request Routing to your web worker (hosting the MVC5 application).

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

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