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. :)