Search code examples
.netactive-directoryguidsid

User GUID when disconnected from network


I can use this code to get the GUID and SID of the currently logged in user:

UserPrincipal user = System.DirectoryServices.AccountManagement.UserPrincipal.Current;
Console.WriteLine("{0}, {1}", user.Guid, user.Sid);

However, if the user logs in without being connected to the network, that code fails ("The server cannot be contacted."). I can get the SID of that user, even if they aren't connected, like this:

WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
Console.WriteLine("{0}", identity.User.Value);

But the WindowsIdentity object doesn't contain a GUID, as far as I can tell.

How can I get the GUID of the current user, if they aren't connected to the network?

NOTE: This is a desktop application, not a web app.


Solution

  • The Security Identificator (SID) is part of each process token, it's the real user identificator. When you are in WorkGroup it's stored in the SAM (Part of the registry) of each computer, when you are in domain it's stored as an attribute of an user object in Active-Directory.

    The GUID is also an attribute of an user object in Active-Directory, but it's main purpose it to insure the unicity of this object.

    As far as I understand the GUID is not part of Windows security, so I'am not surprise that's it's not available in disconected mode.