我编写代码以获取AD中特定组中的用户列表,但我将emailAddress
属性设为NULL
请帮忙吗?
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;
}