我可以使用此代码获取当前登录用户的 GUID 和 SID:
UserPrincipal user = System.DirectoryServices.AccountManagement.UserPrincipal.Current;
Console.WriteLine("{0}, {1}", user.Guid, user.Sid);
但是,如果用户在未连接到网络的情况下登录,则该代码将失败(“无法联系服务器。”)。即使他们没有连接,我也可以获得该用户的 SID,如下所示:
WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
Console.WriteLine("{0}", identity.User.Value);
但据我所知,WindowsIdentity 对象不包含 GUID。
如果当前用户未连接到网络,如何获取当前用户的 GUID?
注意:这是一个桌面应用程序,而不是一个网络应用程序。