Accessing Unity Account data in Unity Editor

I’ve tried looking for an answer to this, but it’s very difficult to find the right combination of keywords to find what I’m looking for.

Basically, I’d like to know whether there’s a way to access the information of the currently logged in user in the Unity Editor. In other words, I was wondering whether I can find the Unity account username of the developer currently logged in the editor. Obviously, this is to be used only in the Editor scope, I’m not expecting it to work in-game.

Thanks in advance.

Unity uses a UnityConnect class which contains UserInfo of the logged in user. All these classes and structs are present under UnityEditor.Connect namespace and they are marked as internal so are not available publicly. But using reflection you can get the required info. The code for it will be something like:

Assembly assembly = Assembly.GetAssembly(typeof(UnityEditor.EditorWindow));
    object uc = assembly.CreateInstance("UnityEditor.Connect.UnityConnect", false, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null, null);

    // Cache type of UnityConnect.
    Type t = uc.GetType();

    // Get user info object from UnityConnect.
    var userInfo = t.GetProperty("userInfo").GetValue(uc, null);

    // Retrieve user id from user info.
    Type userInfoType = userInfo.GetType();
    string userId = userInfoType.GetProperty("userId").GetValue(userInfo, null) as string;

It was tested in 5.6.0f3 and may stop working in future.

Also some other properties from UserInfo struct are: userName, displayName, primaryOrg, organizationForeignKeys, accessToken. All these are string type.

There also is a bool loggedIn property in UnityConnect class which can be used to check if user is logged in or not. Just for the reference, there are bool workingOffline and bool online properties too.