20

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 Silverlight application will be hosted on a static HTML file.

Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
huseyint
  • 14,953
  • 15
  • 56
  • 78

8 Answers8

28

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
  • 281
  • 3
  • 2
27

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
  • 44,143
  • 8
  • 72
  • 99
  • Nice answer! I will probably (and unfortunately) mark this answer as accepted if there won't be any creative answer (hack?) – huseyint Jul 24 '09 at 11:29
7

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.

Code Maverick
  • 20,171
  • 12
  • 62
  • 114
Michael Maddox
  • 12,331
  • 5
  • 38
  • 40
3

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
  • 8,474
  • 7
  • 46
  • 64
  • 2
    While this looks like it will work, you cannot rely on this mechanism for security, such as SSO authentication. – Thorarin Nov 04 '10 at 12:05
  • @Thorarin: This would work fine for Windows/Negotiate or NTLM (what I think of for SSO) authentication or even Basic. All we're talking about here is getting the username back to the Silverlight client. What one does with this information in Silverlight is somewhat dubious. Any type of authentication/authorization work is typically done on the server - esp when SL is so sandboxed. However, I'm not going to tell the OP not to do this since we don't know the whole story. Plus, the OP seems to be looking for creative hacks. – Aardvark Oct 18 '12 at 13:08
2
Environment
    .GetFolderPath(Environment.SpecialFolder.Personal)
    .Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries)[2];
Code Maverick
  • 20,171
  • 12
  • 62
  • 114
ywm
  • 411
  • 1
  • 4
  • 4
0

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
  • 1,329
  • 1
  • 8
  • 13
  • 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 Jul 24 '09 at 15:31
0

// 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];

0

well , here is how you can get it wihtout wcf

http://www.codeproject.com/Articles/47520/Silverlight-Windows-User-Identity-Name

Zakos
  • 1,492
  • 2
  • 22
  • 41