70

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?

Machavity
  • 30,841
  • 27
  • 92
  • 100
Dylan Wheeler
  • 6,928
  • 14
  • 56
  • 80
  • Current user? It's a huge security risk when that's possible in JavaScript. – Rob W Mar 01 '12 at 14:37
  • 3
    Username in context to what ? – aziz punjani Mar 01 '12 at 14:38
  • 3
    When you are signed in to your computer, you have a user name. This is what I wish to get. – Dylan Wheeler Mar 01 '12 at 14:39
  • 1
    How do you expect the current user to be set? This may be a duplicate of http://stackoverflow.com/questions/2604399/is-there-a-way-to-get-ssl-certificate-details-using-javascript or others (and the answer is generally you can't ... unless you control the server side and want to embed it as javascript or what not) or may be a duplicate of http://stackoverflow.com/questions/2604399/is-there-a-way-to-get-ssl-certificate-details-using-javascript (note that if I understand at least one of the answers, this won't work unless you're running the javascript as a firefox add on) – Foon Mar 01 '12 at 14:42

5 Answers5

94

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.

Surreal Dreams
  • 26,055
  • 3
  • 46
  • 61
  • 27
    How is a server-side script going to get more information about the current user of the client OS? – Dagg Nabbit Mar 01 '12 at 15:03
  • 1
    You could run a script on localhost...? I made that suggestion before the poster made the note that he wants the client OS username. – Surreal Dreams Mar 01 '12 at 15:31
  • 4
    @DaggNabbit It's pretty common to establish the user's identity on the server side, using SSPI or GSSAPI. – Simon Brangwin Oct 21 '13 at 05:41
  • 3
    @SimonBrangwin, never heard of those. Why not add an answer showing how you'd use one of those on a web server to get information like the client's username? I'd be very interested to see how that would work. – Dagg Nabbit Oct 21 '13 at 20:49
  • Are we able to get current user name now ?? is there any enhancement in jquery ?? @SurrealDreams – Spartan Oct 06 '16 at 08:38
  • 1
    @thanga - jQuery is still JavaScript. This is not going to change. – Surreal Dreams Oct 06 '16 at 17:41
40

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" />
Greg
  • 31,180
  • 18
  • 65
  • 85
  • 2
    I didn't quite undestand the ASP.Net example. Does it get the username that's logged in the .Net system, or the local machine username (that's what the question is about)? – igorsantos07 Oct 25 '13 at 14:20
  • It depends what authentication mode you use. If you are using Windows Security option User.Identity.Name will return the Windows username – Greg Oct 26 '13 at 15:02
14

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.

Dagg Nabbit
  • 75,346
  • 19
  • 113
  • 141
  • 1
    The browser does indeed have access to the system's username (or any system info for that matter) - however, the browser doesn't expose that to the JS engine. – Snuffleupagus Mar 01 '12 at 14:50
8

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

-3

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.

MarredCheese
  • 17,541
  • 8
  • 92
  • 91
Kosmi
  • 11
  • 1