By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,528 Members | 930 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,528 IT Pros & Developers. It's quick & easy.

How to get windows username using javascripts?

P: 8
Hi.,
i dont know how to get windows username using javascript..
can someone help me please..

thank you in advance,
Mar 14 '07 #1
Share this Question
Share on Google+
16 Replies

acoder
Expert Mod 15k+
P: 16,027
Welcome to TSDN.

Javascript is for the client-side browser scripting. It shouldn't have anything to do with the operating system. That would be a huge security hole if Javascript could access the Windows username.
Mar 14 '07 #2

P: 8
Hi.,
It's possible to access the windows username using javascripts.
but i dont know that.
can anybody help me please.
Mar 15 '07 #3

acoder
Expert Mod 15k+
P: 16,027
How can you be so sure it's possible unless you've seen it? Show me an example.

The only possibility I can think of is through ActiveX (if even that).
Mar 15 '07 #4

NeoPa
Expert Mod 15k+
P: 31,770
I'm afraid I know nothing about Javascript (except that I would guess there is some similarity to Java ;)) but I do know that the Windows Username is always set in the environment variable "USERNAME". That may help if you have access to the environment in your Javascript code.
PS. As environment variables are amendable by the user, this is not a 100% reliable way to get this but it works fine in most circumstances.
Oct 4 '07 #5

gits
Expert Mod 5K+
P: 5,390
hi ...

i'm with acoder ... you cannot and should not be able to retrieve things that are outside of the browser with standard-javascript. imagine a website that is able to collect such information while you visit it ... nobody wants that ... however: in case you have an evironment that should allow that ... may be an intranet ... then you may give the browser some privileges (activeX or some advanced mozilla privileges) to retrieve such information ... but in a webenvironment where you cannot control browsers, nor the version, nor the privileges or security settings etc. ... you cannot do that ...

kind regards
Oct 4 '07 #6

NeoPa
Expert Mod 15k+
P: 31,770
Sorry guys.
If you think my post is inappropriate then please feel free to delete it. This is not my area of expertise so I may have stumbled in where I shouldn't have.
Oct 4 '07 #7

acoder
Expert Mod 15k+
P: 16,027
I'm afraid I know nothing about Javascript (except that I would guess there is some similarity to Java ;))
JavaScript was named after Java, but otherwise they're very different. See, for example, History and naming.
Oct 4 '07 #8

gits
Expert Mod 5K+
P: 5,390
hi NeoPa,

nope ... your post is not :) ... as we said ... there is no standard way to retrieve such information with javascript ... but according to specific circumstances it may be possible of course. when using IE's activeX controls you may do everything in case you have control to the browsers security settings ... because activeX-controls can break out of the browsers javascript-sandbox since they might be written in every language that support to create an activex-control, so that such a control might get access to everything at your machine out of a webpage. this could be a good idea when creating intranet-applications for example.

kind regards
Oct 4 '07 #9

NeoPa
Expert Mod 15k+
P: 31,770
JavaScript was named after Java, but otherwise they're very different. See, for example, History and naming.
Thanks for that acoder.
I've used VBA and a little VBScript. They ARE similar. I'd just assumed the relationship was similar for Java & JavaScript.
Oct 4 '07 #10

acoder
Expert Mod 15k+
P: 16,027
Thanks for that acoder.
I've used VBA and a little VBScript. They ARE similar. I'd just assumed the relationship was similar for Java & JavaScript.
Don't worry, you're by no means the first. The naming was unfortunate. Apparently, Java was the buzz word at the time.

The Java forum get a lot of threads regarding JavaScript problems which get transferred here with r035198x's standard response:
Java != Javascript
Oct 4 '07 #11

P: 6
Hi All,

Firstly, let me agree that this functionality would be a security risk. However if you're a domain/system admin and it's for a local site or a site you have admin access to, it may be possible.

I'm looking into possible ways of doing this myself for auto-authentication on an intranet.

I have a couple of possible solutions, but both have problems. I'll let you know if I come up with something better, but these ideas may help others come up with something.

Solution 1:
Use an ActiveX control, or try to get a visual basic script in a page to get the environment variable.

Problems: Cross browser compatibility - it only works in internet explorer (this won't work in Firefox for example). Also, you need to click the toolbar up the top to allow the control to work. I don't like these. One of my software/system key design principles is "the user is stupid". Based on this principle, assume the user either won't click that toolbar, or will click block.

Solution 2:
Write a cookie from Windows when the user logs in with the user's details, then get the web page retrieve the data from that cookie.

Problems:
Cross browser and operating system issues. Firefox has cookies in a different location to IE etc. Even worse, Firefox on Vista even has a different location to Firefox on XP etc. And just when you get it working, something new will come out to mess it all up again. Also, some users may put cookie restrictions on, which may cause issues.

Any thoughts? I'll keep thinking of ways to do it, but a different approach to whatever the problem is may be best.

Chris Fry
Oct 15 '07 #12

gits
Expert Mod 5K+
P: 5,390
hi ...

in case it is an intranet-environment and you could have control to the browsers and its security settings and restrict them to your needs ... then you simply should support only one solution, lets say the IE version with activeX. you may try to get enhanced mozilla privilegs too, therefore you must edit the about:config and allow mozilla to access os things, probably you need a security certificate at the serverside etc. ... but in case you want the users to have whatever browser they want and whatever security-settings they want ... i think you will have more problems then leaving it as it is ... with a seperate intranet-login i guess. users that want to be logged in with ther windows auth 'should' use 'your' browser ... and the ohers have to re-login ... you cannot have a generic way ... since every browser uses its own specifics ... what about opera? ... or safari? ... in case you could restrict to the use of a specific browser you may have luck with your approach ... in case you could not ... then i think it will be a neverending story ...

kind regards
Oct 15 '07 #13

P: 15
Try this,
Its should work

var wshshell=new ActiveXObject("wscript.shell");
var username=wshshell.ExpandEnvironmentStrings("%usern ame%");
Oct 15 '07 #14

NeoPa
Expert Mod 15k+
P: 31,770
Try this,
Its should work
Expand|Select|Wrap|Line Numbers
  1. var wshshell=new ActiveXObject("wscript.shell");
  2. var username=wshshell.ExpandEnvironmentStrings("%username%");
Bear in mind that while %USERNAME% is fine for simple identification (Hello Mr Jones), it shouldn't generally be used for security purposes. Very few users are so computer illiterate nowadays that they don't know how to change an environment variable, which is all it takes to confuse that code into treating you as an adminstrator or the company CEO.
*EDIT*
Actually they are not as rare as that. Nevertheless, this method should be treated with caution as there is a fairly straightforward method to beat it.
Oct 15 '07 #15

P: 1
Far too late to help abhishekitb, but it can be used by somebody else who asks the same question.

Even if environment variables work well, as NeoPa said, this works too :
Expand|Select|Wrap|Line Numbers
  1. var ActX=new ActiveXObject("wscript.network");
  2. alert(ActX.username);
Note that in the same way, you can get the computer and the domain name.
Expand|Select|Wrap|Line Numbers
  1. alert(ActX.computername);
  2. alert(ActX.userdomain);
Tested on IE... 8... !!!, under Windows XP (sorry, nothing else at hand atm).
Mar 28 '14 #16

NeoPa
Expert Mod 15k+
P: 31,770
People find these threads all the time Eldann, so abhishekitb not benefiting isn't the end of the world. Still worth posting :-)
Mar 29 '14 #17

Post your reply

Sign in to post your reply or Sign up for a free account.