One of the objects in this.Request.Properties (key "MS_HttpContext") has a .Request.UserHostAddress, but I'm not sure it's being populated propertly, and probably not available when self hosting.
Update: This does indeed work and contains the client IP address, it just looked a bit weird when I did this at home (I got "::1" which I believe is IPv6 loopback?).
This works fine for webhosted scenarios. If you want to get the client address
in self-hosted scenarios, you need to look for the
RemoteEndpointMessageProperty in the property bag.
if (this.Request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name)) {
RemoteEndpointMessageProperty prop;
prop = (RemoteEndpointMessageProperty)this.Request.Properties[RemoteEndpointMessageProperty.Name];
Console.WriteLine(prop.Address);
}
Does the RemoteEndpointMessageProperty take the "X_FORWARDED_FOR" server variable into account? Otherweise I won't get the real client Ip but the proxy ip.
This works fine for webhosted scenarios. If you want to get the client address, you need to look for the
RemoteEndpointMessageProperty in the property bag.
This worked fine in the WCF Web API preview 6, but I don't think this property is there in the MVC beta, thats why I didn't recommend it.
I just tried this in a controller now and it doesn't exist, am I missing something?
Sorry, I missed the qualifier "in self-hosted scenarios" in my original reply (I edited it to correct). To get the client address in all scenarios, you'd need code similar to the one below.
No, it doesn't. You only get the ip from where the connection to the service originated. If the proxy added any headers, you can retrieve them via this.Request.Headers, but nothing forces the proxy to add any information about the client (and many don't,
for security reasons, so it's impossible for the server to find out the actual client IP).
Marked as answer by soernt on Feb 28, 2012 06:59 PM
Since that is my own proxy, i will have that information. At least, as you mentioned, from the connecting client (be it another proxy or a real client).
soernt
Member
33 Points
9 Posts
How to get the client IP address for the current controller call.
Feb 26, 2012 05:27 PM|LINK
Hi there,
what is the way to get the client IP address? In mvc I get it via:
string remoteAddress = request.UserHostAddress;
string xForwardedFor = request.ServerVariables["X_FORWARDED_FOR"];
it there something simular wihin the Web API?
- Sörnt
SiggiGG
Member
265 Points
105 Posts
Re: How to get the client IP address for the current controller call.
Feb 26, 2012 08:39 PM|LINK
One of the objects in this.Request.Properties (key "MS_HttpContext") has a .Request.UserHostAddress, but I'm not sure it's being populated propertly, and probably not available when self hosting.
((System.Web.HttpContextWrapper)this.Request.Properties["MS_HttpContext"]).Request.UserHostAddress
SiggiGG
Member
265 Points
105 Posts
Re: How to get the client IP address for the current controller call.
Feb 27, 2012 09:08 AM|LINK
Update: This does indeed work and contains the client IP address, it just looked a bit weird when I did this at home (I got "::1" which I believe is IPv6 loopback?).
CarlosFiguei...
Member
99 Points
19 Posts
Microsoft
Re: How to get the client IP address for the current controller call.
Feb 28, 2012 12:03 AM|LINK
This works fine for webhosted scenarios. If you want to get the client address in self-hosted scenarios, you need to look for the RemoteEndpointMessageProperty in the property bag.
if (this.Request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name)) { RemoteEndpointMessageProperty prop; prop = (RemoteEndpointMessageProperty)this.Request.Properties[RemoteEndpointMessageProperty.Name]; Console.WriteLine(prop.Address); }soernt
Member
33 Points
9 Posts
Re: How to get the client IP address for the current controller call.
Feb 28, 2012 04:44 AM|LINK
Thanks for the answers.
Does the RemoteEndpointMessageProperty take the "X_FORWARDED_FOR" server variable into account? Otherweise I won't get the real client Ip but the proxy ip.
SiggiGG
Member
265 Points
105 Posts
Re: How to get the client IP address for the current controller call.
Feb 28, 2012 09:22 AM|LINK
This worked fine in the WCF Web API preview 6, but I don't think this property is there in the MVC beta, thats why I didn't recommend it.
I just tried this in a controller now and it doesn't exist, am I missing something?
CarlosFiguei...
Member
99 Points
19 Posts
Microsoft
Re: How to get the client IP address for the current controller call.
Feb 28, 2012 03:18 PM|LINK
Sorry, I missed the qualifier "in self-hosted scenarios" in my original reply (I edited it to correct). To get the client address in all scenarios, you'd need code similar to the one below.
private string GetClientIp(HttpRequestMessage request) { if (request.Properties.ContainsKey("MS_HttpContext")) { return ((HttpContext)request.Properties["MS_HttpContext"]).Request.UserHostAddress; } else if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name)) { RemoteEndpointMessageProperty prop; prop = (RemoteEndpointMessageProperty)this.Request.Properties[RemoteEndpointMessageProperty.Name]; return prop.Address; } else { return null; } }soernt
Member
33 Points
9 Posts
Re: How to get the client IP address for the current controller call.
Feb 28, 2012 03:57 PM|LINK
Thanks for the answer!
CarlosFiguei...
Member
99 Points
19 Posts
Microsoft
Re: How to get the client IP address for the current controller call.
Feb 28, 2012 05:21 PM|LINK
No, it doesn't. You only get the ip from where the connection to the service originated. If the proxy added any headers, you can retrieve them via this.Request.Headers, but nothing forces the proxy to add any information about the client (and many don't, for security reasons, so it's impossible for the server to find out the actual client IP).
soernt
Member
33 Points
9 Posts
Re: How to get the client IP address for the current controller call.
Feb 28, 2012 06:58 PM|LINK
Thanks for that Carlos.
Since that is my own proxy, i will have that information. At least, as you mentioned, from the connecting client (be it another proxy or a real client).