Can I find out userid of the remote person logged into windows desktop?

Viewed 143

I am running a Java Desktop appplication on one of our windows machine.

And to make any manual changes in our application, one of our Support persons remote login from his machine using mstsc. (Let's say "Tim")

Here is the catch: For remote connection we use "MysysUser" and password for connection.

Now, there is a requirement of logging in who is making the changes. Is there a way of finding out who the remote user is which is not "MysysUser ? It is "Tim".

In my Java code, i want to find out the name Tim.

Thanks for your time and help.

1 Answers

You can use this class to get the name of the user logged on the machine of Tim, like this example, this feature use native features from windows.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;

public class UserName {
    public static void main(String[] args) throws Exception{
        UserName user = new UserName();
        System.out.println(user.getUserLoggedFromIP("localhost"));
        System.out.println(user.getUserLoggedFromIP("IP_MACHINE_OF_TIM"));
    }

    public String getUserLoggedFromIP(String ip) throws Exception {
        Process p = Runtime.getRuntime().exec("query user /server:" + ip);
        p.waitFor(200, TimeUnit.MILLISECONDS);

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                p.getInputStream()));

        String userActive = reader.lines().filter(x -> x.toUpperCase().contains("ACTIVE"))
                .findFirst()
                .orElse(null);

        if(userActive != null){
            return userActive.trim().split(" ")[0];
        }else{
            return "";
        }
    }
}
Related