1 0 Tag Archives: ip
post icon

Silverlight Tip of the Day #10 – Converting Client IP to Geographical Location

In Tip of the Day #9 I showed you how you can obtain your clients IP addresses through either server side script or a simple web service call. In this tip I will show you how to take that IP address to determine the geographical location of the user that has that IP.

To accomplish this we simply make a WebClient call to a public based web service that will look up the IP address and return the geographical information that is contained in their database.

An example free web service that provides this data is http://ipinfodb.com. The url we pass is in this format http://ipinfodb.com/ip_query.php?ip=YOURIP. Replace YOURIP with your actually IP, copy and paste this URL into the IE address bar and you will see your results.

To do this through Silverlight I set up a WebClient call with the url in the format I showed you above:

string url = "http://ipinfodb.com/ip_query.php?ip={0}"; 
string reqUrl = string.Format(url, ipAddress); 
 
WebClient client = new WebClient(); 
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(downloadInfoCompleted); 
client.DownloadStringAsync(new Uri(reqUrl), null);

 

The method downloadInfoCompleted() is called when the WebClient query has completed. Using XML Linq I extract the data I want from the results as seen here:

void downloadInfoCompleted(object sender, DownloadStringCompletedEventArgs e) 
{ 
    XDocument xml = XDocument.Parse(e.Result); 
 
    XElement ip = xml.Element("Response").Element("Ip"); 
    XElement countryCode = xml.Element("Response").Element("CountryCode"); 
    XElement countryName = xml.Element("Response").Element("CountryName"); 
    XElement regionCode = xml.Element("Response").Element("RegionCode"); 
    XElement regionName = xml.Element("Response").Element("RegionName"); 
    XElement city = xml.Element("Response").Element("City"); 
    XElement postalCode = xml.Element("Response").Element("ZipPostalCode"); 
    XElement timeZone = xml.Element("Response").Element("Timezone"); 
    XElement gmtOffset = xml.Element("Response").Element("Gmtoffset"); 
    XElement dstOffset = xml.Element("Response").Element("Dstoffset"); 
    XElement latitude = xml.Element("Response").Element("Latitude"); 
    XElement longitude = xml.Element("Response").Element("Longitude"); 
}

 

Any number of these values could return null. To get the actual value I first check if it is null else I obtain the data from XElement.Value as seen here:

if(null != ip) 
        IPTB.Text = "Your IP = "+ip.Value.ToString();

Demo:

[silverlight: Tip10_GeoLocation.xap]

Thank you,

–Mike

Leave a Comment
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

Leave a Comment