views:

6237

answers:

4

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 Internet Explorer. Getting the identity from server side with ASP.NET is not an option, this SL application will be hosted on a static HTML file.

+6  A: 

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.

CraigTP
Nice answer! I will probably (and unfortunately) mark this answer as accepted if there won't be any creative answer (hack?)
huseyint
A: 

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.

SmartyP
Actually we have initially go for a JavaScript solution, and it worked. But it requires the user to accept a bunch of security dialogs which is far from ideal.
huseyint
+8  A: 

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.

Richard
+1  A: 

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");
 }
Aardvark