简体   繁体   English

如何将“用户”对象从aspx传递到silverlight 4

[英]How do I Pass a 'user' object from aspx to silverlight 4

First, we are constrained to using an existing web framework that handles authentication and authorization. 首先,我们只能使用处理身份验证和授权的现有Web框架。 The web project uses forms authentication and writes to an encrypted cookie. 该Web项目使用表单身份验证并写入加密的cookie。 The user information is exposed to the aspx pages as a property: 用户信息作为属性公开给aspx页面:

LoggedInUser.Current

This has several properties including the userId and role list. 它具有几个属性,包括userId和角色列表。

I've looked at using initParams, but haven't been very successful there (edit: I couldn't do it dynamically originally). 我已经看过使用initParams了,但是在那儿还没有很成功(编辑:我原本不能动态地做到这一点)。 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. 我已经创建了一个具有[Key]属性的简单POCO实体,但至少需要能够将userId从aspx页面传递到嵌入的silverlight。

What is the easiest way to pass a dynamic object from aspx to silverlight 4? 将动态对象从aspx传递到silverlight 4的最简单方法是什么?

Thank you slugster: 谢谢slugster:

Set up the initParams on the aspx page 在aspx页面上设置initParams
<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. (虽然不漂亮,但是可以工作)然后使用Slugster的示例在Silverlight端使用值。

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). 警告:通过init参数传递用户信息会将信息以纯文本格式显示给查看页面的用户(他们只需要查看源代码即可)。 We ended up using an authentication domain service and using the same user object as the aspx 我们最终使用了身份验证域服务,并使用了与aspx相同的用户对象

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. 在Silverlight应用程序的启动对象的构造函数中,您可以读取托管Silverlight应用程序的aspx页面的查询字符串。

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. 因此,您可以通过查询字符串传递ID,然后使用该ID调用wcf服务,该服务将返回您的POCO或您可以通过电线进行序列化的任何对象。

an example of getting the query string is shown below. 下面显示了获取查询字符串的示例。

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

Hope this helps 希望这可以帮助

You have 3 basic options: 您有3个基本选项:

  • use the initParams parameter of the Silverlight object plugin 使用Silverlight对象插件的initParams参数
  • use the Silverlight-javascript bridge to access values held within the aspx part of the page 使用Silverlight-javascript桥来访问页面的aspx部分中保存的值
  • use a WCF (or asmx) webservice call to get the info you require 使用WCF(或asmx)网络服务调用获取所需的信息

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. 使用initParams,您将通过逗号分隔的值列表进行发送,该列表在Silverlight控件的Application_Startup部分中StartupEventArgs键/值集合StartupEventArgs参数。 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). MyAppDetails是一个可全局访问的(在SL应用程序中)静态对象)。 Using this method i can dynamically populate the initParams at runtime within the aspx page. 使用此方法,我可以在运行时在aspx页面中动态填充initParams。

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). 的几个环节让你开始是我的一些前的答案在这里 ,并在这里 ,并回答我自己的问题就同一主题很久以前这里 (这个答案是更倾向于做事的风格WCF)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM