我在活动目录中有一个安全组(如下图所示),该组具有与之关联的电子邮件地址.如何获取该组的电子邮件地址? GroupPrincipal对象上没有任何电子邮件地址属性.
这就是我检索所有组的方式:
using (PrincipalContext context = new PrincipalContext(DirectoryContextType, Domain)) {
using (var groupSearcher = new GroupPrincipal(context)) {
using (var searcher = new PrincipalSearcher(groupSearcher)) {
foreach (GroupPrincipal group in searcher.FindAll()) {
//How do I get the e-mail address?
}
}
}
}
最佳答案 如果您想通过帐户管理执行此操作,则需要
make a new class that exposes that property.
[DirectoryObjectClass("group")]
[DirectoryRdnPrefix("CN")]
public class GroupPrincipalsEx : GroupPrincipal
{
public GroupPrincipalsEx(PrincipalContext context) : base(context) { }
public GroupPrincipalsEx(PrincipalContext context, string samAccountName)
: base(context, samAccountName)
{
}
[DirectoryProperty("mail")]
public string EmailAddress
{
get
{
if (ExtensionGet("mail").Length != 1)
return null;
return (string)ExtensionGet("mail")[0];
}
set { this.ExtensionSet("mail", value); }
}
}