Wednesday, March 27, 2013

Getting IP address of client in Silverlight


The one thing that I have been searching so far is how to get IP address of the client in my application. I want to keep track on where a particular application, that I have created is being used. Well this is important, I think there are some girls who are using my applications and this is the best way to hunt them all down. It was long search though, to find the answer, a perfect answer. But in the end I am writing it all down. Hell Yeah!!!
So to begin with, we need to write some JavaScript, my love. Let us create a new application, a silverlight application to rehearse the methodology of getting IP address.  Add the following script block anywhere in the aspx page of your project.

   1: <script language="JavaScript" type="text/javascript">
   2: var ip_list = {};
   3: function FoundIp(ip, type) {
   4:     if (type == 0) {
   5:         type = 4;
   6:         if (ip.indexOf(':') != -1) type = 6;
   7:     }
   8:     ip_list[ip] = type;
   9: }
  10:  
  11: function MyIpIs() {
  12:     for (ip in ip_list) {
  13:         return ip.toString();
  14:     }
  15: }    
  16: </script>

If this isn’t enough, add one more script tag below it.

   1: <script language="JavaScript" src="http://only-ip4.wimip.fr/ip.php" type=""></script>

The tag above uses a 3rd party api to retrieve the IP of the client via returning an XML file, that the script we’ve written above will parse to pick out the IP. The function MyIpIs() will return the first IP it will get from the list it will search into. It will throw the IP which we will catch from silverlight. As you have already seen how to call Javascript’s function from silverlight in one of my previous post, Cookies in silverlight.
We are going to do the same here also. We will be calling the function MyIpIs() from, you can guess it, yes you guessed it correct, MainPage. On the constructor of main page we make the call to the function as:

   1: var ipAddress = HtmlPage.Window.Invoke("MyIpIs");
   2: IpContainer.Text = ipAddress.ToString();

In the XAML I have my textblocks defined. Here is the whole XAML:

   1: <UserControl x:Class="GettingIP.MainPage"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
   5:     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
   6:     mc:Ignorable="d"
   7:     d:DesignHeight="300" d:DesignWidth="400">
   8:  
   9:     <Grid x:Name="LayoutRoot" Background="White">
  10:         <TextBlock Text="IP is: "/>
  11:         <TextBlock x:Name="IpContainer" Margin="30 0 0 0"/>
  12:     </Grid>
  13: </UserControl>

F5…and yes it’s your IP, believe it or not. The above is the simple way to get your IP via silverlight. Next we will be calculating the geolocation of the user. Stay tuned…

Share this post

2 comments

  1. I am Using Silverlight5(Silverlight Application) and got stuck between nowhere.
    How to get Client IP address?

    ReplyDelete
    Replies
    1. Hi, Can you please tell me what is your problem exactly?

      Delete

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
© 2013 Neelesh Vishwakarma
Posts RSS Comments RSS
Back to top