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!

18 Comments so far »

  1. Maciek said

    am April 13 2008 @ 4:54 am

    Hi Michael, your IP adress hint is lovely, although I can’t get it to work for some reason.

    string ip = e.InitParams["IpAddress"];

    this line throws an exception on me as it’s not receiving any InitParams, have I missed something?

  2. Michael Sync said

    am April 13 2008 @ 8:37 am

    Hello Maciek,

    What exception are you getting? runtime exception or compile-time exception? It would be great if you can show me the full exception message..

  3. Maciek said

    am April 13 2008 @ 8:59 am

    “An exception of type ‘System.Collections.Generic.KeyNotFoundException’ occurred in mscorlib.dll but was not handled in user code

    Additional information: The given key was not present in the dictionary.”

    I’ve tried “Running to cursor” in the Default.aspx.cs file but it appears to never execute that code (and thus never send any InitParams to App.xaml.cs ?), even after setting Default.aspx as the start page. (I’ve copied the content of Testpage.aspx into Default.aspx, so my control is called Xaml1 but that’s the only difference I think.

  4. Maciek said

    am April 13 2008 @ 4:32 pm

    Ok I’ve finally figured it out and it’s working nicely. The error was on my end (as usual). Default.aspx needs to look like this :

    Test page for obtaining IP adresses and Session IDs

    It’s important that the CodeFile property points to the code file so the IP adress is passed to the App constructor.

    Sincirely

  5. Maciek said

    am April 13 2008 @ 4:33 pm

    whoops looks like it doesn’t permit html lol

  6. Wöchentliche Rundablage: Silverlight 2, ASP.NET MVC, .NET 3.5, jQuery, CSS, Powershell | Code-Inside Blog said

    am April 14 2008 @ 5:29 am

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

  7. Weekly Links: Silverlight 2, ASP.NET MVC, .NET 3.5, jQuery, CSS, Powershell | Code-Inside Blog International said

    am April 14 2008 @ 5:38 am

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

  8. Munius said

    am April 19 2008 @ 4:24 am

    Great article. Just the information I was looking for. Thank you.

  9. unruledboy said

    am July 1 2008 @ 8:56 pm

    Hi,
    since SL 2 introduced System.Net namespace, why not just use System.Net.Dns functions to get the client ip? the SL runs at client side, right?

  10. Michael Sync said

    am July 1 2008 @ 9:10 pm

    I think System.Net.Dns doesn’t give me the correct IP. Yes. Silverlight runs at client-side that’s why I think we can’t get the IP Address. Like Javascript, we are not able to get the client ip address from Javascript.

  11. unruledboy said

    am July 1 2008 @ 9:13 pm

    Hi,

    I don’t get it. for a winform/c++/vb/delphi program, they can all get the client ip through winsock which System.Net.Dns internally wraps. since the SL runs at client machine, why?

  12. Michael Sync said

    am July 1 2008 @ 9:19 pm

    no.. Silverlight is not windows application. It runs on the browser but it runs within sandbox. so, it has some limitations.. if you are familiar with web client-scripting technology such as Javascript or VBScript, you will understand those limitations…

  13. unruledboy said

    am July 1 2008 @ 9:28 pm

    Hi,

    sorry to bother you. I wrote a few asp.net with ajax/js, js does not achieve something because it simply does not support. if for security reason, I could understand, but since SL 2 include the System.Net namespace, it should work. but I also read some articles of yours, tim heuer and other SL guys, mentioned about some mechanism that work with browser such as WebClient not using socket but the browser’s plug-in functions, I might understand, though.

    after all, could we conclude that SL is NOT simply a client “object”, but a “limited proxy” between server and client? it could not fully runs as other activexs that run at browser, and could not do those things like activex made by vb6 that could utilize winsock etc?

  14. Michael Sync said

    am July 1 2008 @ 9:52 pm

    Yes. you are right. It is not like ActiveX made by VB or etc. It’s the limited version of ActiveX. but Silverlight did support Socket but it has some limitations.

  15. Mike said

    am September 9 2008 @ 6:40 pm

    So how is the System.Collections.Generic.KeyNotFoundException solved?

  16. Mike said

    am September 11 2008 @ 8:32 pm

    What should the Default.aspx file include?

  17. Marc Roussel said

    am October 9 2008 @ 3:46 am

    this :

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

    Doesn’t work for me. The intellisence doesn’t see “Silverlight1″

  18. Erikitha said

    am October 17 2008 @ 12:12 pm

    Thanks for this example, you helped me to finish easily a task.
    I needed to get a session variable. I followed your example.

    Amazing,

    Erika.

Comment RSS · TrackBack URI

Leave a comment

Name: (Required)

eMail: (Required)

Website:

Comment: