簡體   English   中英

獲取本地和域帳戶用戶所屬的組

[英]Get groups a user belongs to for both local and domain accounts

我正在嘗試列出給定用戶所屬的所有組,並且我想同時支持本地計算機帳戶和域帳戶。 我可以使用以下代碼獲取域帳戶的組:

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()的調用也沒有返回任何結果。 如何獲得對計算機帳戶和域帳戶均可靠的特定用戶所屬的組?

多虧了我從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之前, ListAllGroups用戶名中刪除域,因為在某些情況下,提供給我的功能會將其添加到用戶名之前。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM