简体   繁体   English

C#控制台应用程序-获取当前用户广告组

[英]C# Console Application - Get Current User AD Groups

I'm writing a script to get a list of the current user's AD Groups and creates a .txt file to a Path. 我正在编写一个脚本,以获取当前用户的广告组的列表,并创建指向路径的.txt文件。

I've had a look around and it seems like I should be using these references: 我环顾四周,似乎应该使用这些引用:

using System.DirectoryServices; using System.DirectoryServices.AccountManagement;

I'm also using this: 我也在用这个:

UserPrincipal user = UserPrincipal.FindByIdentity(new PrincipalContext(ContextType.Domain, "<Domain>"), IdentityType.SamAccountName, "<UserName>");
foreach (GroupPrincipal group in user.GetGroups())
{
    Console.Out.WriteLine(group);
}

But this doesn't quite list all the groups that the user should be in. 但这并没有列出用户应加入的所有组。

Is there something I'm missing? 有什么我想念的吗?

I use this block of code to get the user groups: 我使用以下代码块来获取用户组:

        String domainName = @"<your domain>";
        String username = domainName + @"\<your username>";
        PrincipalContext thisDomain = new PrincipalContext(ContextType.Domain);

        UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(thisDomain, username);

        PrincipalSearchResult<Principal> userGroups = userPrincipal.GetAuthorizationGroups();

        foreach (Principal principal in userGroups.OfType<GroupPrincipal>())
        {
            Debug.WriteLine(principal.Name);
        }

As far as I can tell, it lists all the groups the user is a member of by comparison to what is held in Active Directory and looking at the user object in the MMC snap in 据我所知,通过与Active Directory中保存的内容进行比较,并列出了MMC管理单元中的用户对象,它列出了用户所属的所有组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM