Compartilhar via


Getting User Context in Silverlight 2

I recently completed work on an internal Silverlight app.    The project was a blast – it really reminded me of how coding could be fun again.   One of the surprising gaps I found in Sliverlight was the inability to get the current user context (e.g. determine the login domain and userid of the person viewing the page).    I checked with the product team and they confirmed that this is not (yet?) a feature of Silverlight.   While this feature may be provided at a later date, I needed it now.   I was able to develop a workaround using ASP.NET’s LOGON_USER server variable using the following:

<%

String userName = Request.ServerVariables["LOGON_USER"];

UID.Value = userName;

%>

    <form id="form1" runat="server" style="height:100%;">

        <input type="hidden" name="userContext" id="UID" runat="server" />

        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

        <center>

            <table border="0">

                <tbody>

                    <tr>

                        <td width="10"></td>

                        <td width="auto">

                            <div id="silverlightControlHost">

                                <script type="text/javascript">

                                        document.write ("<object data=\"data:application/x-silverlight,\" type=\"application/x-silverlight-2-b1\" width=\"1150px\" height=\"810px\">");

                                        document.write ("<param name=\"source\" value=\"ClientBin/MyApp.xap\"/> ");

                                        document.write ("<param name=\"background\" value=\"white\" />");

                                        document.write ("<param name=\"onerror\" value=\"onSilverlightError\" />");

                                        document.write("<param name=\"initParams\" value=\"userID=" + form1.UID.value + "\"");

                                        document.write("<\object>");

                                </script>

                            </div>

                        </td>

                        <td width="10"></td>

                    </tr>

                </tbody>

            </table>

        </center>

    </form>

It ain’t pretty but it works.  :)

Comments

  • Anonymous
    May 16, 2008
    PingBack from http://www.basketballs-sports.info/better-basketball/?p=1004

  • Anonymous
    May 18, 2008
    Yeah, Of course it works. Since silverlight can get prameters by passing the vlaue into his parameter format. And Cons still!

  • Anonymous
    May 26, 2008
    Can you simply use object tag or asp:Silverlight control directly instead of using document.write?  

  • Anonymous
    June 03, 2008
    Michael: its probably possible, I haven't looked into it.   I needed a quick and easy way to pass a dynamic parameter in and document.write seemed the easiest way to do it.   Again, it isn't pretty but it works.

  • Anonymous
    July 05, 2008
    you can access the membership info via a webservice like described here: http://blogs.msdn.com/brada/archive/2008/05/03/accessing-the-asp-net-authentication-profile-and-role-service-in-silverlight.aspx I think you can get querystring variables with this: HtmlPage.Document.QueryString["key"] (as in page.htm?key=pwd)