Get the user name

From CodeCodex

Revision as of 05:37, 18 August 2009 by Techmind (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Implementations

Java

In application : <highlightsyntax language="java122"> public class Test {

   public static void main(String args[]) {    
     System.out.println( System.getProperty("user.name") );
   }

} </highlightsyntax> will print the current user. You can't use this technique to secure your application since it is very to spoof. You just need to specify a "user.name" from the command line. <highlightsyntax language="java122"> > java -Duser.name=Elvis Test Elvis </highlightsyntax> As an alternative with JDK1.5, <highlightsyntax language="java122"> public class Test {

 public static void main(String args[]) {
   com.sun.security.auth.module.NTSystem NTSystem = new
           com.sun.security.auth.module.NTSystem();
   System.out.println(NTSystem.getName());
   System.out.println(NTSystem.getDomain());
 }

} </highlightsyntax>


In Applet there is no way unless you ask for it or use a signed applet. If you have access to a server-side, something like an ASP page can be used to detect the current NT user name if the client and the server are configured correcty (SSO).


See this related HowTo for a JSP hack!