获取用户所属的本地和域帐户的组

Gil*_*ish 3 c# account-management

我正在尝试列出给定用户所属的所有组,并且我想同时支持本地计算机帐户和域帐户。我可以使用以下代码获取域帐户的组:

public void ListAllGroupsDomain()
{
    ListAllGroups(ContextType.Domain,
                  "myDomain",
                  "myDomainUser",
                  "myDomainPassword");
}

public void ListAllGroups(ContextType contextType
                          string name,
                          string username,
                          string password)
{
    using (var context = new PrincipalContext(contextType,
                                              name,
                                              username,
                                              password))
    {
        using (var findByIdentity = UserPrincipal.FindByIdentity(context, "testedit"))
        {
            if (findByIdentity != null)
            {
                var groups = findByIdentity.GetGroups(context);
                var results = groups.Select(g => g.Name).ToArray();
                Console.WriteLine("Listing {0} groups", results.Count());
                foreach (var name in results)
                {
                    Console.WriteLine("{0}", name);
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试调用机器本地的帐户,我会收到一个 COM 异常,说我的用户名或密码不正确(它们不是):

public void ListAllGroupsMachine()
{
    ListAllGroups(ContextType.Machine,
                  "myMachine",
                  "myMachineUser",
                  "myMachinePassword");
}
Run Code Online (Sandbox Code Playgroud)

我猜我对机器帐户错误地使用了 FindByIdentity。所以我尝试了一种不使用 FindByIdentity 的稍微不同的方法:

public void ListAllGroups2(ContextType contextType
                          string name,
                          string username,
                          string password)
{
    using (var context = new PrincipalContext(contextType,
                                              name,
                                              username,
                                              password))
    {
        using (var userContext = new UserPrincipal(context))
        {
            var results = userContext.GetGroups();
            Console.WriteLine("Listing {0} groups:", results.Count());
            foreach (var principal in results)
            {
                Console.WriteLine("{0}", principal.Name);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这个版本没有抛出任何异常,但调用GetGroups()也没有返回任何结果。如何获取特定用户所属的对计算机和域帐户均可靠的组?

Gil*_*ish 6

感谢我从/sf/answers/257700971/收集到的一些帮助,我发现我做错了什么。我错误地设置了 PrincipalContext。我不需要给它域、用户名或密码,只需提供 ContextType。以下代码适用于本地计算机帐户和域帐户:

public void ListAllGroupsDomain()
{
    ListAllGroups(ContextType.Domain, "myDomainUsername");
}

public void ListAllGroupsMachine()
{
    ListAllGroups(ContextType.Machine, "myMachineUsername");
}

public void ListAllGroups(ContextType contextType, string userName)
{
    using (var context = new PrincipalContext(contextType))
    {
        using (var findByIdentity = UserPrincipal.FindByIdentity(context, userName))
        {
            if (findByIdentity != null)
            {
                var groups = findByIdentity.GetGroups(context);
                var results = groups.Select(g => g.Name).ToArray();
                Console.WriteLine("Listing {0} groups", results.Count());
                foreach (var name in results)
                {
                    Console.WriteLine("{0}", name);
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要确保做的唯一一件事是在我将它传递给之前从用户名中删除域,ListAllGroups因为在某些情况下,提供给我的函数会将它添加到用户名之前。