【问题标题】:Get groups a user belongs to for both local and domain accounts获取用户所属的本地和域帐户组
【发布时间】:2013-04-17 05:41:16
【问题描述】:

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

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);
                }
            }
        }
    }
}

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

public void ListAllGroupsMachine()
{
    ListAllGroups(ContextType.Machine,
                  "myMachine",
                  "myMachineUser",
                  "myMachinePassword");
}

我猜我对机器帐户错误地使用了 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);
            }
        }
    }
}

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

【问题讨论】:

    标签: c# account-management


    【解决方案1】:

    感谢我从 https://stackoverflow.com/a/3681442/1222122 收集到的一点帮助,我弄清楚了我做错了什么。我错误地设置了 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);
                    }
                }
            }
        }
    }
    

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

    【讨论】:

    • 这正是我正在寻找的 atm,但我对 .FindByIdentity 和 .GetGroups 调用的访问速度真的很慢。您是否遇到过性能问题?
    • 对你来说有多慢,斯派克?我在处理这个问题时尝试了几种不同的方法,我似乎记得有些几乎是瞬时的,有些需要 10 多秒。不过,时间太长了,我不能告诉你更多。
    • FindByIdentity 大约需要 4 秒(使用 ContextType.Machine),GetGroups 大约需要 6 秒。不过,我不在域中 - Windows 8.1,通过 IISExpress 运行。我想,是时候进行一些缓存了。
    猜你喜欢
    • 2012-06-28
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 2023-03-21
    • 2011-04-10
    • 2017-10-30
    相关资源
    最近更新 更多