Is it possible to get the currently logged in user\'s username with Silverlight? You can assume that user has Windows OS and the Silverlight application is hosted in Interne
// a small improvement of Code Maverick answer using System.IO.Path.DirectorySeparatorChar rather // then //
Environment .GetFolderPath(Environment.SpecialFolder.Personal) .Split(new[] { System.IO.Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries)[2];
You can manage to get by this way.
1) Create asp.net web service application.
2) Implement web service and method to call from silverlight applicaton.
[WebMethod]
public string GetClientUserName()
{
return System.Web.HttpContext.Current.User.Identity.Name.ToString();
}
3) Deploy this web service application on web server. Don't allow anonymous user to access this.
4) Add this service to Silverlight application. (Add service reference)
5) Now, you can call this method and get user name from entire silverlight application.
according to this post its not possible in javascript, so i think your out of luck: http://bytes.com/topic/javascript/answers/616501-how-get-windows-username-using-javascripts
sounds like there is no real way to do it.
Unfortunately, I don't think it's possible.
Although you say that we can assume Windows OS/IE, Silverlight itself certainly doesn't assume this, so most of the normal .NET mechanisms that would ordinarily be available to us to get the current logged on user's name do not exist within the subset of the framework available to Silverlight apps:
ie.
System.Net.CredentialCache.DefaultCredentials
System.Security.Principal.WindowsIdentity.GetCurrent().Name
Environment.UserName
are all unavailable within a Silverlight application, whereas in (say) a Windows Forms Application, each of these mechanisms is available to use.
I suppose it makes sense, really, since there's no guarantee that the Silverlight application is going to be running on top of a Windows/IE platform.
Incidentally, this question was also asked over here:
Current Windows Username and Domain
and that thread seems to confirm that there's no native way to achieve this. The thread then goes on to suggest "injecting" the current ASP.NET user name from the ASP.NET page "hosting" the Silverlight app. into the Silverlight application itself prior to it running in the context of the client's machine. Of course, even if this works, it'll only give you the ASP.NET user name from either ASP.NET's forms or windows based authentication and not the Windows username of currently logged on user of the client machine.
I figured I'd share this code which seems to work (YMMV). It is inspired by CraigTP's answer and his links he provided. I know this doesn't directly answer the part about running in a static web page, but it seems the OP accepted the APSX compromise.
In my ASPX page that hosts Silverlight:
<head>
<!-- ...snip... -->
<script runat="server" language="c#">
void Page_Load()
{
this.UsernameField.Value = User.Identity.Name;
}
</script>
</head>
<body>
<input type="hidden" ID="UsernameField" runat="server" />
<!-- ...snip... -->
</body>
In my silverlight C# code:
private string GetCurrentUserName()
{
HtmlDocument doc = HtmlPage.Document;
if (doc == null)
{
return string.Empty;
}
HtmlElement elm = doc.GetElementById("UsernameField");
if (elm == null)
{
return string.Empty;
}
return elm.GetAttribute("value");
}
The highly voted answers to this question did not help me.
Using an ASP.NET web service, this worked however:
string userName =
System.ServiceModel.ServiceSecurityContext.Current.WindowsIdentity.Name;
And this blog entry is the one that set me on the correct path:
http://rouslan.com/2009/03/12/20-steps-to-get-together-windows-authentication-silverlight-and-wcf-service/
ServiceReferences.ClientConfig needs this:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding ...>
<security mode="TransportCredentialOnly" />
</binding>
</basicHttpBinding>
</bindings>
</system.serviceModel>
And web.config needs this:
<system.web>
<authentication mode="Windows" />
<identity impersonate="false" />
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding ...>
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
</system.serviceModel>
Those are the only noteworthy changes I needed to make to an otherwise working Silverlight application that previously used anonymous access for web services in IIS.