Sometimes you want to know the username of a user that's logged on to your domain. This specially the occasion for intranet applications. Let's assume we are building an intranet application because this is the only occasion where a NTLM username is valid I guess. There is no way we can get the NTLM Username directly from within Silverlight yet. So we have two options left: Client Side Javascript and Server Side scripting.
Client Side
Let's begin with the Client Side Javascript. At least we should start investigating it. The first piece of code I found makes use of ActiveX, see the following:
var wshshell=new ActiveXObject("wscript.shell"); var username=wshshell.ExpandEnvironmentStrings("%username%");
Sadly it throws an error, not a good start. Google again. Not many options left, most people just say it isn't possible. After a while I found the following:
var wshNetwork = new ActiveXObject("WScript.Network"); var username = WshNetwork.UserName;
But this also throws an error. So I guess we are left to the Server Side solution.
Server Side
So what we have left is Server Side scripting. We can again take different approaches like calling the server side through Ajax but I took the easy way, by just generating the NTLM Username into the HTML. We can make use of the InitParams to pass some startup parameters to the Silverlight control. The InitParams is a string that has comma-separated name-value pairs that are separated by the equals sign. So this could look like the following in HTML:
<param name="InitParams" value="name1=value1,name2=value2" />
What we do in the ASPX page where the Silverlight control is hosted, add the following code in the Page_Load:
1 if (User != null && User.Identity != null && User.Identity.IsAuthenticated) 2 Silverlight1.InitParameters = string.Format("User.Identity.Name={0}", User.Identity.Name);
In Silverlight we can read this value in the App.xaml.cs.
1 private void Application_Startup(object sender, StartupEventArgs e) 2 { 3 string userName = e.InitParams["User.Identity.Name"]; 4 this.RootVisual = new Page(); 5 }
The generated HTML looks like the following:
<param name="InitParams" value="User.Identity.Name=DOMAINNAME\Username"></param>




- Govardhan
Where is 'User' declared/defined in the following line?
if (User != null && User.Identity != null && User.Identity.IsAuthenticated)
Thanks!
I want to find the logged in user name in silverlight.
My Silverlight app connect to SQL server using WCF & LINQ. I am running a WCF service in anonymous mode. When I try to change this to ‘windows authentication’ to get the user credentials. It is not allowing me to change.
Can you provide some sample example for your Server Side solution.
Thank you very much for help.
Silverlight using WCF with Windows Authentication: http://mark.mymonster.nl/2009/05/12/silverlight-using-wcf-with-windows-authentication/
Where do you get the reference to Silverlight1? I am sure it's a reference to the Silverlight Object, but how do you get that object from the server side?
This was the time when we still had a Serverside control to render the object html.
I gather from your comments on 29th Jan 2010 (20:14) that this no longer works in SL3/SL4? Can you confirm? thanks
It's true, the exact implementation will be difficult because the Silverlight server-control doesn't exist in ASP.NET anymore. But the concept, of filling initParams still works.
I'm trying to do something like in a intranet
I'm developing an application in silverlight
is it possible to get the username of the client's windows and use the same user name in a variable in sql?
You can use this example. With the exception that the server control to host Silverlight no longer exists. But if you are able to get the username inside the initParams, than the usage in a variable is almost peanuts.
Please let me know if you need more help.
works well but only for local machine.
What I want is to get the username from the client
the string that I quoted above does not work for the xaml.cs
works only for CS file
it is possible to collect through the server the username of the local machine?
thanks