c# – 使用DirectoryServices.AccountManagement,如何获取活动目录安全组的电子邮件地址?
作者:互联网
我在活动目录中有一个安全组(如下图所示),该组具有与之关联的电子邮件地址.如何获取该组的电子邮件地址? 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); }
}
}
标签:c,net,active-directory,directoryservices 来源: https://codeday.me/bug/20190704/1372081.html