How to get the name of the current Windows user in JavaScript

JavascriptHtmlWindows

Javascript Problem Overview


I was wondering how I would get the name of the current user in JavaScript as part of an HTML document.

In Java, one would type System.getProperty("user.name"); to achieve this. What is the alternative to this in JavaScript?

Javascript Solutions


Solution 1 - Javascript

JavaScript runs in the context of the current HTML document, so it won't be able to determine anything about a current user unless it's in the current page or you do AJAX calls to a server-side script to get more information.

JavaScript will not be able to determine your Windows user name.

Solution 2 - Javascript

There is no fully compatible alternative in JavaScript as it posses an unsafe security issue to allow client-side code to become aware of the logged in user.

That said, the following code would allow you to get the logged in username, but it will only work on Windows, and only within Internet Explorer, as it makes use of ActiveX. Also Internet Explorer will most likely display a popup alerting you to the potential security problems associated with using this code, which won't exactly help usability.

<!doctype html>
<html>
<head>
    <title>Windows Username</title>
</head>
<body>
<script type="text/javascript">
    var WinNetwork = new ActiveXObject("WScript.Network");
    alert(WinNetwork.UserName); 
</script>
</body>
</html>

As Surreal Dreams suggested you could use AJAX to call a server-side method that serves back the username, or render the HTML with a hidden input with a value of the logged in user, for e.g.

(ASP.NET MVC 3 syntax)

<input id="username" type="hidden" value="@User.Identity.Name" />

Solution 3 - Javascript

If the script is running on Microsoft Windows in an HTA or similar, you can do this:

var wshshell=new ActiveXObject("wscript.shell");
var username=wshshell.ExpandEnvironmentStrings("%username%");

Otherwise, as others have pointed out, you're out of luck. This is considered to be private information and is not provided by the browser to the javascript engine.

Solution 4 - Javascript

I think is not possible to do that. It would be a huge security risk if a browser access to that kind of personal information

Solution 5 - Javascript

Working for me on IE:

<script type="text/javascript">
  var WinNetwork = new ActiveXObject("WScript.Network");
  document.write(WinNetwork.UserName);
</script>

...but ActiveX controls needs to be on in security settings.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDylan WheelerView Question on Stackoverflow
Solution 1 - JavascriptSurreal DreamsView Answer on Stackoverflow
Solution 2 - JavascriptGregView Answer on Stackoverflow
Solution 3 - JavascriptDagg NabbitView Answer on Stackoverflow
Solution 4 - Javascriptuser1233246View Answer on Stackoverflow
Solution 5 - JavascriptKosmiView Answer on Stackoverflow