Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current Windows user name within Silverlight

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.

like image 450
huseyint Avatar asked Jul 24 '09 09:07

huseyint


3 Answers

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.

like image 31
CraigTP Avatar answered Nov 15 '22 13:11

CraigTP


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.

like image 51
Richard Avatar answered Nov 15 '22 11:11

Richard


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.

like image 43
Michael Maddox Avatar answered Nov 15 '22 13:11

Michael Maddox