【问题标题】:How can I get the local group name for guests/administrators?如何获取来宾/管理员的本地组名称?
【发布时间】:2011-03-13 21:00:04
【问题描述】:

问题:

我使用在 http://support.microsoft.com/kb/306273

添加一个windows用户。 问题是我需要将用户添加到组中,但组名已本地化。

例如MS-example 使用英文计算机,这意味着您可以像这样获取来宾组:

grp = AD.Children.Find("Guests", "group")

但在非英语计算机上,“Guest”组名是本地化的,例如在我的德语操作系统上,Guest 组名是“Gäste”。

这意味着要在我的计算机上运行支持示例,我需要将该行更改为

grp = AD.Children.Find("Gäste", "group")

然后就可以了。

现在,如果操作系统是任何其他语言,我如何找到来宾用户的名称? 或者我如何从 sid 中​​获取来宾用户名?

注意:.NET 2.0,而不是 3.0 或 3.5

【问题讨论】:

    标签: c# .net vb.net active-directory directoryservices


    【解决方案1】:

    正如您所指出的,组的名称会根据系统语言进行本地化。

    对于像“管理员”和“访客”这样的“知名”组,您应该根据 SID 进行检索。来宾的 SID 是:

    S-1-5-32-546
    

    这里有一个众所周知的 SID 列表:

    http://support.microsoft.com/kb/243330

    可以在here找到从SID获取组名的代码

    【讨论】:

      【解决方案2】:

      你可以使用这段代码,返回值对于非英文系统是正确的:

      var guestsGroup = new SecurityIdentifier(WellKnownSidType.BuiltinGuestsSid, null).Translate(typeof(NTAccount)).Value;
      

      【讨论】:

        【解决方案3】:

        通过 SID 查找帐户是最好的方法。这有点做作,但它的工作方式是这样的:

        • Administrator 帐户的 SID始终S-1-5-21 开头并以 -500 结尾。中间的所有其他内容都是随机的(域的 SID)。

        • Guest 帐户的 SID始终S-1-5-21 开头并以 -501 结尾。

        描述此问题的 Microsoft 知识库文章可在 here 获取。

        要查找这些帐户,您必须枚举本地计算机上的所有帐户,并找出以这些数字开头和结尾的 SID。一旦它们匹配,您就拥有了内置帐户。不是最好的方法,但它确实有效。

        Security Settings\Local Policies\Security Options 下还有一个名为 Accounts: Rename administrator accountAccounts: Rename guest account 的组策略设置强>。我无法找到这些设置在注册表中的存储位置,但如果您能够找到并查找它们,您很可能能够获得这两个帐户的“官方”名称。

        【讨论】:

          【解决方案4】:

          This page 有一些代码用于获取用户详细信息并进行检查。

          这段代码:

          public IdentityReferenceCollection GetUserGroups()
          {
              System.Security.Principal.WindowsIdentity currentUser =
                                System.Security.Principal.WindowsIdentity.GetCurrent();
              return currentUser.Groups;
          }
          

          返回当前用户的组。

          更多关于WindowsIdentityclass 的详细信息可以在here 中找到,Groups 属性here

          【讨论】:

            【解决方案5】:

            您应该能够使用 WindowsIdentity 和 WindowsPrincipal 类:

            Dim currentIdentity as WindowsIdentity = WindowsIdentity.GetCurrent()
            Dim currentPrincipal as WindowsPrincipal = New WindowsPrincipal(currentIdentity)
            
            If currentPrincipal.IsInRole(WindowsBuiltInRole.Guest) Then
               Foobar()
            End If
            

            没关系,我看到您实际上是在尝试将用户添加到组中。

            【讨论】:

              【解决方案6】:

              如果你想获得管理员组,你可以使用这个代码:

              public static DirectoryEntry GetLocalAdminstratorGroup()
              {
                using (var WindowsActiveDirectory = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"))
                  {
                    return WindowsActiveDirectory.Children.Find(GetLocalizedAdministratorGroupName(), "group");
                  }
              }
              
               //Localized == Language Independent
              public static string GetLocalizedAdministratorGroupName()
              {
               //For English Windows version, this equals "BUILTIN\Administrators".
               var adminGroupName = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null).Translate(typeof(NTAccount)).Value;
              
               //Remove the "BUILTIN\" part, get the local name of the group
               return adminGroupName.Split('\\')[1];
              }
              

              如果你还想枚举它(比如你需要一个用户名),你可以这样做,使用之前的方法:

              DirectoryEntry AdminGroup = GetLocalAdminstratorGroup();
              
              object members = AdminGroup.Invoke("members", null);
              foreach (object groupMember in (IEnumerable)members)
              {
                DirectoryEntry member = new DirectoryEntry(groupMember);
                Console.WriteLine(member.Name);
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-12-23
                • 2018-11-16
                相关资源
                最近更新 更多