This tip shows you how to obtain your client’s IP address when using Silverlight. Unfortunately there is no way to accomplish this directly from Silverlight. It is also not supported via JScript so communicating between Silverlight and JScript will also not help here.
There are really two solutions you can use to accomplish this:
#1: Use a web service.
#2: Use server side script
Solution #1: Web Service Technique
The first way I have found to accomplish this is to have your Silverlight application communicate directly with a WCF service where the web services returns to the Silverlight client the IP address of the client.
To do this the web service simply checks for one of the following server variables:
1. HTTP_X_FORWARDED_FOR – This is used to obtain the originating IP address of a client that connected through a proxy.
2. REMOTE_ADDR – Used to obtain the client IP address if a proxy was not used.
Place the following code in your web service to retrieve and return the clients IP address to your Silverlight app:
[OperationContract]
public string GetCustomerIP()
{
string CustomerIP = System.Web.HttpContext.Current.Request.UserHostAddress;
/* NOTE: This is another way to do it but the one method is more straight forward:
if( HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
CustomerIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
else if (HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] != null)
CustomerIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
*/
return CustomerIP;
}
In my Silverlight code I call the web service setting a TextBlock with the results:
MainPage.xaml.cs:
public MainPage()
{
InitializeComponent();
ServiceReferenceIP.ServiceIPClient client = new ServiceReferenceIP.ServiceIPClient();
client.GetCustomerIPCompleted +=
new EventHandler<ServiceReferenceIP.GetCustomerIPCompletedEventArgs>
(client_GetCustomerIPCompleted);
client.GetCustomerIPAsync();
}
void client_GetCustomerIPCompleted(object sender, ServiceReferenceIP.GetCustomerIPCompletedEventArgs e)
{
string ip = e.Result;
YourIP.Text = "Your IP is " + ip;
}
MainPage.xaml:
<TextBlock x:Name="YourIP" Text="Your IP is..." />
If you are not familiar with Silverlight WCF web service check out my blog on WCF (Tip #11) which will cover this extensively.
Solution #2: Server Side Script
In this technique we will leverage a Silverlight object control parameter called “initParams” to pass the IP address to the Silverlight client. To start, in the code behind of your web page add the following code in your Page_Load() event to get the IP address via the Server Variable REMOTE_ADDR.
public string InitParam;
protected void Page_Load(object sender, EventArgs e)
{
InitParam = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
Next, pass this variable to your Silverlight application via the InitParams:
<param name="initParams" value="IPAddress=<%=InitParam%>"/>
Now, in your App.xaml.cs you can retrieve this parameter in the Application_Startup() event. Pass this variable along to your MainPage:
private void Application_Startup(object sender, StartupEventArgs e)
{
string ipAddress = e.InitParams["IpAddress"];
this.RootVisual = new MainPage(ipAddress);
}
Finally, open up MainPage.xaml.cs and add a parameter to the constructor to receive the IP address:
public MainPage(string ipAddress)
{
}
Source: Tip9_MyIP
Demo:
[silverlight: tip9_MyIP.xap]
Thanks,
–Mike







Hi Mike,
This is a great tip! You can also get the client’s IP address using a Server Side Include:
We use this a lot for our Silverlight apps:
http://support.vertigo.com/getip/
-Mike
I connected but it showed 10.0.0.1
Can you share the code you use?
Thanks!
I can think of 2 other ways to do it as well.
Way #1 was presented by Michael Sync a while ago : http://michaelsync.net/2008/04/07/silverlight-2-beta1-url-referrer-screen-resolution-clients-data-time-and-ip-address
Way #2 : Initiate a socket connection and get the client’s IP and port on the server
Thanks for the pointer Maciek, I’ll add that technique to my blog as well.
Hi Mike, here is the code we use to generate the IP address using the REMOTE_ADDR field with Server Side Includes enabled:
“”
Note: the code snippet may get sucked out when the comment is posted to your blog. If see, please send me an email and I will follow up.
-Mike
Yep, sucked out. My email is silverlightgamer AT gmail DOT com
Thanks.
Could you not use:
System.Web.HttpContext.Current.Request.UserHostAddress
In the WebService?
Yes, thanks. I addded that to the blog also!
This works really fine with wcf service, but for those who works with ria services it wont work. ria needs the aspNetCompatibilityEnabledto set “true” in the web.config, but with setting aspNetCompatibilityEnabled=”true” this ip app wont work. Is there a way to use this example without setting aspNetCompatibilityEnabled to “false”?
This is nice post, it served my purpose. I like the second method because to make a call (though it’s for one time only) to WCF/Web service just to get IP is not a good use of it.
Share your thoughts