UserPrincipal EmailAddress为NULL

时间:2018-03-26 18:08:02

标签: c# active-directory

我编写代码以获取AD中特定组中的用户列表,但我将emailAddress属性设为NULL

enter image description here

请帮忙吗?

public List<UserPrincipal> Groups(string grpName)
{
    List<UserPrincipal> users = new List<UserPrincipal>();
    PrincipalContext context = new PrincipalContext(ContextType.Domain, "Company", userName: "XXXXXXXX", password: "XXXXXX");

    using (var searcher = new PrincipalSearcher())
    {               
        var sp = new GroupPrincipal(context, grpName);
        searcher.QueryFilter = sp;

        var group = searcher.FindOne() as GroupPrincipal;

        if (group == null)
            Console.WriteLine("Invalid Group Name: {0}", grpName);

        foreach (var f in group.GetMembers())
        {
            var principal = f as UserPrincipal;

            if (principal == null || string.IsNullOrEmpty(principal.Name))
                continue;

            users.Add(principal);
            //Console.WriteLine("{0}", principal.Name);
        }
    }

    return users;
}

0 个答案:

没有答案