如何在c#中使用AD组名查找Active Directory组电子邮件地址



我有一个名为Stack Over Flow IT的Active Directory组。需要找到AD组邮件,如stackoverflowit@stackoverflow.com。不需要查找AD用户列表

如何查找AD组邮件地址?

或者如何通过AD组邮件地址查找AD组名称?

// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// find the group in question
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere");

我无法使用上述group实例代码找到Active Directory组电子邮件地址。

组邮件

你可以这样做:

PropertyValueCollection email = ((DirectoryEntry) group.GetUnderlyingObject()).Properties["mail"];

如果你有RSAT可用,你可以验证你的代码(在powershell):

get-adgroup -Identity "Stack Over Flow IT" -properties mail | select name,mail | sort mail

通过邮件查找组

这是完整的反向方法:

// replace stuff inside [] to match your environment
DirectoryEntry root = new DirectoryEntry("LDAP://dc=[YOUR DC]", [username], [password], AuthenticationTypes.Secure);
DirectorySearcher groupSearcher = new DirectorySearcher(root);
groupSearcher.Filter = "(mail=stackoverflowit@stackoverflow.com)";
groupSearcher.PropertiesToLoad.Add("name");
foreach (SearchResult groupSr in groupSearcher.FindAll())
{
ResultPropertyValueCollection groupName = groupSr.Properties["name"];
// do something with finding
}

最新更新