by Bobbi Perreault
25. November 2009 01:10
Share on FacebookI needed to know the Active Directory username of the current user from my Silverlight app. The Silverlight control is running from a Sharepoint site.
It seems that this isn't as easy as you would think. I found several ways, but the one I ended up using wasn't one of them.
- Page.User.Identity.Name.Split('\\')[1]; - this would work from code behind or even in alligator tags in front end - but my Sharepoint page wouldn't allow them.
- var wshshell=new ActiveXObject("wscript.shell");
var username=wshshell.ExpandEnvironmentStrings("%usern ame%");
For i As Integer = 0 To Request.ServerVariables.Count - 1
Response.Write(Request.ServerVariables.Keys(i) & " : <br /> [ " & Request.ServerVariables(i) & " ] <br /> ")
Next
This one is totally hard to do under any type of security scenario.
- HttpContext.Current.Profile.UserName - again, this would work from code behind or even in alligator tags in front end - but my Sharepoint page wouldn't allow them.
- <script type="text/vbscript" language="VBscript">
Dim X
set X = createobject("WSCRIPT.Network")
dim U
U=x.UserName
'MsgBox "username: " & U
</script>
<script type="text/javascript" language="Javascript">
var a = U;
$("#name").text(a.toString());
</script>
Hard to even print these two that use active x! But for the sake of completeness, here they are.
AND - The way I used:
When my Silverlight app is loaded, the first thing I do is send a web service request to add a list item to a log list. The list has only one field, that is Title. So I add an "application opened" record to the log. What I get in return is an xml confirmation that the record was added - the confirmation contains the name of the logged in user - which is their active directory name. I just parse the correct element out "ows_Author" using XLinq and have what I needed.
Is this weird? Cause I wouldn't be surprised! Anyway, it works for me.