【问题标题】:Get Guest built-in account name in C#在 C# 中获取 Guest 内置帐户名称
【发布时间】:2019-11-09 18:47:46
【问题描述】:

我想用 C# 获取 Guest 内置帐户名。因为不同地区的语言不同,Guest 仅适用于英语版本的 Windows,例如在西班牙语 Windows 上,Guest 为Invitado。 我想要做的是获取名称并使用我获得的名称编辑帐户,例如设置密码或添加一些组。

我已经尝试过这段代码:

            var sGuest = new SecurityIdentifier(WellKnownSidType.AccountGuestSid, null);
            PrincipalContext systemContext = null;
            systemContext = new PrincipalContext(ContextType.Machine);
            guestPrincipal = UserPrincipal.FindByIdentity(systemContext, IdentityType.Name, sGuest.ToString());

我得到这个错误:

System.ArgumentNullException: 'The domainSid parameter must be specified for creating well-known SID of type AccountGuestSid.

【问题讨论】:

  • 值得指出的是Display&Login Name与FileSystem中的名称不同。即,如果我重命名用户,用户个人资料文件夹通常会保留旧名称。它根本无法移动/重命名而不会冒问题。 |然而,对于 Guest 来说,应该有一个特殊的变量需要检查。它应该标有 isGuestAccount = true 之类的东西。
  • 另一个问题是有时文件夹名称已本地化,但仅在资源管理器中查看时。配置文件的文件夹是“用户”。它作为“用户”写入文件系统。但它显示为 Users 或 Benutzer(德语)、usuario(我希望是西班牙语)或其他类似本地化的内容。
  • 是的,这都是真的,我只是想按我说的那样从该地区获取访客用户的名称。

标签: c# administrator system-administration


【解决方案1】:

本地访客帐户的 SID 采用 S-1-5-21domain-501 的形式,其中 domain 是机器的 SID。因为构建来宾帐户 SID 需要域 SID,所以如果 domainSid 参数为空,SecurityIdentifier 的构造函数将失败。您可以通过 WMI 获取机器 SID,并将其作为 domainSid 参数传递给 SecurityIdentifier 构造函数。

或者,可以在没有机器 SID 的情况下获取来宾帐户。这样做的一种方法是使用PrincipalSearcher 来识别其 SID 是众所周知的访客帐户 SID 的用户帐户,并查询其 Name 属性以获取该帐户的本地名称:

// using System.Security.Principal;
// using System.DirectoryServices.AccountManagement;
new PrincipalSearcher(new UserPrincipal(new PrincipalContext(ContextType.Machine)))
                .FindAll()
                .Single(a => a.Sid.IsWellKnown(WellKnownSidType.AccountGuestSid))
                .Name // returns the local account name, e.g., 'Guest'

【讨论】:

  • 如何联系您?如果你有空,我有一些问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多