Silverlight 2 (beta1): URL Referrer, Screen Resolution, Client’s Data Time and IP Address

It’s simple but it’s something that we need to use in the most of projects.

1. How to get the referrer in Silverlight?


var referrer = HtmlPage.Document.GetProperty("referrer");

2. How to get the screen resolution?

I’m not sure whether we have the managed code for that or not. but we can use Javascript to get the screen resolution of the user’s monitor. Please feel free to let me know if you have another approach.


var screenHeight = HtmlPage.Window.Eval("screen.height");
var screenWidth = HtmlPage.Window.Eval("screen.width");

3. How to get the client’s date and time?

Like normal .NET application, we have System.DataTime class in Silverlight.


System.DateTime.Now

4. How to get the IP Address of Client’s machine?

I don’t think that it’s possible to get the IP Address of client’s machine using Javascript or client script. You will need to use the server-side script for that. In my example, I will show the way how to get the IP address in ASP.NET.

The following codes can be used for getting the IP Address or host name but you should note that you won’t be able to get the actual address if the proxy server is hiding those addresses.

  • Request.ServerVariables(“REMOTE_HOST”)
  • HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]
  • HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  • Request.UserHostAddress()
  • Request.UserHostName()
  • string strHostName = System.Net.Dns.GetHostName();
    string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

I will use HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] in my example. We have to pass this address as parameters to Silverlight content.

4.1. Add asp:Silverlight control in Default.aspx. If you are facing some problems in adding this control, please read this post. Actually, it’s better if we are able to use in TestPage.aspx but we can’t because we want to add <% %> in asp:Silverlight control. As this control is a server control, we can’t add inline server-side code. If you do that, you will get this error “Server tags cannot contain <% … %> constructs”.

4.2. Go to Default.aspx.cs and add the following code in Page_load event.


Silverlight1.InitParameters = "IpAddress=" + HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

4.3. Go to App.xaml.cs and Write the following code in Application_Startup event.


private void Application_Startup(object sender, StartupEventArgs e) {
// Load the main control
string ip = e.InitParams["IpAddress"];
this.RootVisual = new Page(ip);
}

4.4. You need one parameter to the constructor of Page class.

public Page(string ipAddress) {

}

That’s all. This is the way how you can get the IP address of client’s machine.

Additionally, if you want to get the browser information, you can use HtmlPage.BrowserInformation object. Using this object, you will be able to get the version of browser, the user agent and etc.

Hope you will find it useful.

Happy Silverlighting!