How to get the ip address of the client posting to a service.

1,663 views
Skip to first unread message

Alex C

unread,
May 23, 2012, 1:00:13 PM5/23/12
to servic...@googlegroups.com
Hey, 

I'm new to ServiceStack and I need to find out the IP address of the client talking to the service.
Basically I have a service talking to different clients, and I need to save each client's IP server side.

Thanks,
Alex.

Demis Bellot

unread,
May 23, 2012, 1:23:42 PM5/23/12
to servic...@googlegroups.com
You can access the IpAddress which is available from the RequestContext from your services with:

var ipAddr = base.RequestContext.IpAddress;

You can get access more info about the Request from the IHttpRequest:

var httpReq = base.RequestContext.Get<IHttpRequest>();

Finally you can get access to the original ASP.NET HttpRequest with:

var aspnetReq = (HttpRequest).OriginalRequest;

Cheers,

Alex C

unread,
May 23, 2012, 1:33:19 PM5/23/12
to servic...@googlegroups.com
Great, thanks.
I'll look into that.
Alex.

Alex C

unread,
May 23, 2012, 5:09:55 PM5/23/12
to servic...@googlegroups.com
Hi,

I'm having trouble with this.
The base.RequestContext.Get<IHttpRequest>().UserHostAddress is giving back the actual IP from the service and not the user. I'm not sure how exactly I'm supposed to get the user's ip.

Alex.

On Wednesday, May 23, 2012 1:23:42 PM UTC-4, mythz wrote:

Demis Bellot

unread,
May 23, 2012, 5:19:51 PM5/23/12
to servic...@googlegroups.com
base.RequestContext.Get<IHttpRequest>().UserHostAddress returns the http://msdn.microsoft.com/en-us/library/system.web.httprequest.userhostaddress.aspx (for ASP.NET) which *is* the IP host address of the remote client.

If your services is being accessed through a proxy then this will be the proxy servers IP address and you will need to inspect the X-Forwarded-For HttpHeader to get the remote IPAddress their proxying, An example of how to get that is here:

Reply all
Reply to author
Forward
0 new messages