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!

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

  1. 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. 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. “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. 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. 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?

  6. 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.

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

  8. 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…

  9. 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?

  10. 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.

  11. this :

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

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

  12. 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.

  13. The “Silverlight1.InitParameters ”
    is a SilverLight control you will need to drag and drop into the default.aspx page.

  14. Hi all, i have find a solution in the case you use RIA autentificationServices

    in the web Project, add a string property in Models.User
    and exclude it from the Profile like this :

    [ProfileUsage(IsExcluded = true)]
    public string UserIP { get; set; }

    and add this code in Services.AuthentificationService :

    protected override User GetAuthenticatedUser(System.Security.Principal.IPrincipal principal)
    {
    user.UserIP = HttpContext.Current.Request.UserHostAddress;

    return user;
    }

    then in your silverlight project, you can acces it from eanywhere with

    WebContext.Current.User.UserIP

    Have a nice day! :)

Leave a Reply

Your email address will not be published. Required fields are marked *