How to get the client IP address for the current controller call.http://forums.asp.net/t/1773976.aspx/1?How+to+get+the+client+IP+address+for+the+current+controller+call+Sun, 08 Apr 2012 10:05:02 -040017739764851592http://forums.asp.net/p/1773976/4851592.aspx/1?How+to+get+the+client+IP+address+for+the+current+controller+call+How to get the client IP address for the current controller call. <p>Hi there,</p> <p>what is the way to get the client IP address? In mvc I get it via:</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string remoteAddress = request.UserHostAddress;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string xForwardedFor = request.ServerVariables[&quot;X_FORWARDED_FOR&quot;];</p> <p>it there something simular wihin the Web API?</p> <p>- Sörnt</p> 2012-02-26T17:27:58-05:004851665http://forums.asp.net/p/1773976/4851665.aspx/1?Re+How+to+get+the+client+IP+address+for+the+current+controller+call+Re: How to get the client IP address for the current controller call. <p>One of the objects in this.Request.Properties (key &quot;MS_HttpContext&quot;) has a .Request.UserHostAddress, but I'm not sure it's being populated propertly, and probably not available when self hosting.</p> <p>((System.Web.HttpContextWrapper)this.Request.Properties[&quot;MS_HttpContext&quot;]).Request.UserHostAddress</p> 2012-02-26T20:39:56-05:004852425http://forums.asp.net/p/1773976/4852425.aspx/1?Re+How+to+get+the+client+IP+address+for+the+current+controller+call+Re: How to get the client IP address for the current controller call. <p>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 &quot;::1&quot; which I believe is IPv6 loopback?).</p> 2012-02-27T09:08:48-05:004853741http://forums.asp.net/p/1773976/4853741.aspx/1?Re+How+to+get+the+client+IP+address+for+the+current+controller+call+Re: How to get the client IP address for the current controller call. <p>This works fine for webhosted scenarios. If you want to get the client address <strong>in self-hosted scenarios</strong>, you need to look for the <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.remoteendpointmessageproperty.aspx"> RemoteEndpointMessageProperty</a> in the property bag.</p> <pre class="prettyprint">if (this.Request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name)) { RemoteEndpointMessageProperty prop; prop = (RemoteEndpointMessageProperty)this.Request.Properties[RemoteEndpointMessageProperty.Name]; Console.WriteLine(prop.Address); }</pre> 2012-02-28T00:03:53-05:004853983http://forums.asp.net/p/1773976/4853983.aspx/1?Re+How+to+get+the+client+IP+address+for+the+current+controller+call+Re: How to get the client IP address for the current controller call. <p>Thanks for the answers.</p> <p>Does the RemoteEndpointMessageProperty take the &quot;X_FORWARDED_FOR&quot; server variable into account? Otherweise I won't get the real client Ip but the proxy ip.</p> <p></p> 2012-02-28T04:44:38-05:004854517http://forums.asp.net/p/1773976/4854517.aspx/1?Re+How+to+get+the+client+IP+address+for+the+current+controller+call+Re: How to get the client IP address for the current controller call. <p></p> <blockquote><span class="icon-blockquote"></span> <h4>CarlosFigueira</h4> This works fine for webhosted scenarios. If you want to get the client address, you need to look for the <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.remoteendpointmessageproperty.aspx"> RemoteEndpointMessageProperty</a> in the property bag.</blockquote> <p></p> <p>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.</p> <p>I just tried this in a controller now and it doesn't exist, am I missing something?</p> 2012-02-28T09:22:52-05:004855159http://forums.asp.net/p/1773976/4855159.aspx/1?Re+How+to+get+the+client+IP+address+for+the+current+controller+call+Re: How to get the client IP address for the current controller call. <p>Sorry, I missed the qualifier &quot;in self-hosted scenarios&quot; 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.</p> <pre class="prettyprint">private string GetClientIp(HttpRequestMessage request) { if (request.Properties.ContainsKey(&quot;MS_HttpContext&quot;)) { return ((HttpContext)request.Properties[&quot;MS_HttpContext&quot;]).Request.UserHostAddress; } else if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name)) { RemoteEndpointMessageProperty prop; prop = (RemoteEndpointMessageProperty)this.Request.Properties[RemoteEndpointMessageProperty.Name]; return prop.Address; } else { return null; } }</pre> 2012-02-28T15:18:13-05:004855243http://forums.asp.net/p/1773976/4855243.aspx/1?Re+How+to+get+the+client+IP+address+for+the+current+controller+call+Re: How to get the client IP address for the current controller call. <p>Thanks for the answer!</p> <p></p> 2012-02-28T15:57:30-05:004855351http://forums.asp.net/p/1773976/4855351.aspx/1?Re+How+to+get+the+client+IP+address+for+the+current+controller+call+Re: How to get the client IP address for the current controller call. <p>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).</p> 2012-02-28T17:21:19-05:004855481http://forums.asp.net/p/1773976/4855481.aspx/1?Re+How+to+get+the+client+IP+address+for+the+current+controller+call+Re: How to get the client IP address for the current controller call. <p>Thanks for that Carlos.</p> <p>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).</p> <p></p> 2012-02-28T18:58:44-05:004921554http://forums.asp.net/p/1773976/4921554.aspx/1?Re+How+to+get+the+client+IP+address+for+the+current+controller+call+Re: How to get the client IP address for the current controller call. <pre class="prettyprint">public static string GetClientIp(HttpRequestMessage request) { string ip = string.Empty; if (request.Properties.ContainsKey(&quot;MS_HttpContext&quot;)) { HttpContextBase context = (HttpContextBase)request.Properties[&quot;MS_HttpContext&quot;]; if (context.Request.ServerVariables[&quot;HTTP_VIA&quot;] != null) { // IP ip = context.Request.ServerVariables[&quot;HTTP_X_FORWARDED_FOR&quot;].ToString(); } else { ip = context.Request.ServerVariables[&quot;REMOTE_ADDR&quot;].ToString(); } } return ip; }</pre> <p></p> 2012-04-08T10:05:02-04:00