post icon

Silverlight Tip of the Day #9 – Obtaining Your clients IP Address

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

11 Comments

Leave a comment
  1. Mike Hanley
    30. Apr, 2010 at 5:00 pm #

    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

  2. Snowman
    30. Apr, 2010 at 5:08 pm #

    I connected but it showed 10.0.0.1

    Can you share the code you use?

    Thanks!

  3. Maciek Misztal
    02. May, 2010 at 4:16 am #

    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 :)

  4. admin
    02. May, 2010 at 5:34 am #

    Thanks for the pointer Maciek, I’ll add that technique to my blog as well.

  5. Mike Hanley
    03. May, 2010 at 5:12 pm #

    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

  6. Snowman
    03. May, 2010 at 6:29 pm #

    Yep, sucked out. My email is silverlightgamer AT gmail DOT com

    Thanks.

  7. PeteTheGeek
    05. May, 2010 at 12:39 am #

    Could you not use:
    System.Web.HttpContext.Current.Request.UserHostAddress
    In the WebService?

  8. Snowman
    05. May, 2010 at 10:15 am #

    Yes, thanks. I addded that to the blog also!

  9. J.Akhtar
    14. Sep, 2010 at 3:13 am #

    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”?

  10. Hitesh
    17. Mar, 2011 at 7:18 am #

    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

Trackbacks/Pingbacks

  1. Silverlight Tips of the Day » Silverlight Tip of the Day #10 – Converting Client IP to Geographical Location - 03. May, 2010

    [...] Tips of the Day Silverlight Tip of the Day #9 – Obtaining Your clients IP Address 04/29/2010Silverlight Tip of the Day #8 – Detecting Alt, Shift, Control, Window & Apple Keys [...]