2

First, we are constrained to using an existing web framework that handles authentication and authorization. The web project uses forms authentication and writes to an encrypted cookie. The user information is exposed to the aspx pages as a property:

LoggedInUser.Current

This has several properties including the userId and role list.

I've looked at using initParams, but haven't been very successful there (edit: I couldn't do it dynamically originally). I've created a simple POCO entity with a [Key] attribute, but I need to at least be able to pass the userId from the aspx page to the imbedded silverlight.

What is the easiest way to pass a dynamic object from aspx to silverlight 4?

Thank you slugster:

Set up the initParams on the aspx page
<param name="initParams" value="<%=InitParam%>"/>
in Code behind:

private void LoadSilverlightParams()
    {
        LoggedInUser user = LoggedInUser.Current;
        InitParam = "UserId=" + user.PersonId.ToString() + ",";
        InitParam += "OrganizationId=" + user.OrganizationId.ToString() + ",";
        InitParam += "RoleList=";
        foreach(string s in user.Roles)
        {
            InitParam += s + "|";
        }
        InitParam.Remove(InitParam.Count() - 1);
    }

(not pretty, but it works) Then used Slugster's example to use the values on the Silverlight side.

Warning: Passing user information via init params exposes the info as plain text to the user viewing the page (they just have to view the source). We ended up using an authentication domain service and using the same user object as the aspx

Noel
  • 600
  • 16
  • 37

2 Answers2

0

In the constructor of the startup object of your silverlight application you could read the query string of the aspx page hosting the silverlight app.

So you could pass the id via a query string and then use that id to call a wcf service which would return your POCO or any object u can serialize through the wire.

an example of getting the query string is shown below.

string val = System.Windows.Browser.HtmlPage.Document.QueryString["id"];

Hope this helps

scartag
  • 17,548
  • 3
  • 48
  • 52
  • This is also a viable option (i forgot to include this one in my list). Personally i don't use it because it is slow (anything to do with crossing the javascript bridge or accessing the DOM is slow), and it is prone to URL tampering. For passing one or two non-critical pieces of info though it may be the simplest option. – slugster Dec 28 '10 at 03:17
  • @slugster I offered that since he mentioned that he has tried initParams unsuccessfully. To use WCF he needs to at least have the id to use as a parameter .. so that leaves only initParams and my option ... which i think would feel more natural .. albeit tamper prone :) – scartag Dec 28 '10 at 03:21
  • Thank you. My issue with initParams was that they only passed a string, and the user information is queried from a different location that our application does not have access to. @slugster, using your example I think I will just pass each bit of information as a param and see how that goes. – Noel Dec 28 '10 at 17:04
0

You have 3 basic options:

  • use the initParams parameter of the Silverlight object plugin
  • use the Silverlight-javascript bridge to access values held within the aspx part of the page
  • use a WCF (or asmx) webservice call to get the info you require

With initParams, you send through a comma delimited list of values which becomes available as the key/value collection StartupEventArgs parameter in the Application_Startup part of the Silverlight control. You can then just check for the existence of a specific key and then rehydrate a data object with the values you find:

private void Application_Startup(object sender, StartupEventArgs e)
{
    if (e.InitParams != null && e.InitParams.Count > 0)
    {
        foreach (string key in e.InitParams.Keys)
        {
            switch (key.ToLower())
            {
                case "user":
                    MyAppDetails.UserId = int.Parse(e.InitParams["user"]);
                    break;

                case "year":
                    MyAppDetails.SelectedYear = int.Parse(e.InitParams["year"]);
                    break;

                case "userlogonname":
                    MyAppDetails.UserLogonName = e.InitParams["userlogonname"];
                    break;

                case "currentyear":
                    MyAppDetails.CurrentYear = int.Parse(e.InitParams["currentyear"]);
                    break;

                case "debuglevel":
                    MyAppDetails.DebugLevel = (AppDebugLevel)int.Parse(e.InitParams["debuglevel"] ?? "1");
                    break;

                case "uitheme":
                    MyAppDetails.UITheme = e.InitParams["uitheme"];
                    break;

            }
        }

    }

    this.RootVisual = new MainPage();
}

(MyAppDetails is a globally accessible (within the SL app) static object). Using this method i can dynamically populate the initParams at runtime within the aspx page.

A couple of links to get you started are some former answers of mine here and here, and an answer to my own question on the same topic a long time ago here (this answer is more oriented towards the WCF style of doing things).

Community
  • 1
  • 1
slugster
  • 49,403
  • 14
  • 95
  • 145
  • Thank you for the detailed response. I found a lot of examples of using initParams on the web, but I'm a back to middle tier guy helping out the person doing the silverlight side. I'll give this a shot. One problem that I figured out last night, is that even if I get the UserId I can't query to get the roles associated with the person from our application. I'll pass each bit of discreet info as a param and build the user object on the silverlight side. – Noel Dec 28 '10 at 17:09
  • Warning: Passing user information via init params exposes the info as plain text to the user viewing the page (they just have to view the source). We ended up using an authentication domain service and using the same user object as the aspx. – Noel Mar 01 '11 at 00:49