How to get windows username using javascripts?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abhishekitb
    New Member
    • Mar 2007
    • 8

    How to get windows username using javascripts?

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

    thank you in advance,
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    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.

    Comment

    • abhishekitb
      New Member
      • Mar 2007
      • 8

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

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        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).

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32633

          #5
          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.

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5388

            #6
            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

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32633

              #7
              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.

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Originally posted by NeoPa
                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.

                Comment

                • gits
                  Recognized Expert Moderator Expert
                  • May 2007
                  • 5388

                  #9
                  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

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32633

                    #10
                    Originally posted by acoder
                    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.

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      Originally posted by NeoPa
                      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:
                      Originally posted by r035198x
                      Java != Javascript

                      Comment

                      • chrisfry
                        New Member
                        • Oct 2007
                        • 6

                        #12
                        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

                        Comment

                        • gits
                          Recognized Expert Moderator Expert
                          • May 2007
                          • 5388

                          #13
                          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

                          Comment

                          • KnightHack
                            New Member
                            • Oct 2007
                            • 15

                            #14
                            Try this,
                            Its should work

                            var wshshell=new ActiveXObject(" wscript.shell") ;
                            var username=wshshe ll.ExpandEnviro nmentStrings("% username%");

                            Comment

                            • NeoPa
                              Recognized Expert Moderator MVP
                              • Oct 2006
                              • 32633

                              #15
                              Originally posted by KnightHack
                              Try this,
                              Its should work
                              Code:
                              var wshshell=new ActiveXObject("wscript.shell");
                              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.

                              Comment

                              Working...