在AD工作中,我们有一些启用了邮件的安全组。我正在使用System.DirectoryServices.AccountManagement命名空间:
List<GroupPrincipal> result = new List<GroupPrincipal>();
using (PrincipalContext domain = new PrincipalContext(ContextType.Domain, userinfo[0]))
using (UserPrincipal user = UserPrincipal.FindByIdentity(domain, username))
{
if (user != null)
{
PrincipalSearchResult<Principal> groups = user.GetAuthorizationGroups();
int totalGroupCounter = 0;
StringBuilder output = new StringBuilder();
List<GroupPrincipal> securityGroups = new List<GroupPrincipal>();
List<GroupPrincipal> distributionGroups = new List<GroupPrincipal>();
foreach (Principal group in groups)
{
totalGroupCounter++;
if (((GroupPrincipal)group).IsSecurityGroup.Value)
securityGroups.Add((GroupPrincipal)group);
else
distributionGroups.Add((GroupPrincipal)group);
}
}
}
有了这些信息,找到群组电子邮件地址的正确方法是什么?
答案 0 :(得分:11)
AccountManagement库限制您可以访问的属性。如果要获取组的电子邮件属性,则需要将其强制转换为DirectoryEntry
对象。
PropertyValueCollection email = ((DirectoryEntry)group.GetUnderlyingObject()).Properties["mail"];
if (email.Value != null)
{
// Do something with email property
}
答案 1 :(得分:0)
我认为marc_s是活动目录主题的专家,但是,我也有一个安全组,其中有一个与之关联的电子邮件地址。以下是我能够从中获取电子邮件的方式:
private void GetGroupEmail() {
using (var searcher = new DirectorySearcher()) {
searcher.Filter = "(&(objectClass=group))";
searcher.SearchRoot = entry;
searcher.PropertiesToLoad.Add("mail");
foreach (SearchResult sr in searcher.FindAll()) {
var email = GetSearchResultProperty(sr, "mail");
}
}
}
private string GetSearchResultProperty(SearchResult sr, string propertyName) {
var property = sr.Properties[propertyName];
if (property != null && property.Count > 0) {
return (string)property[0];
} else {
return null;
}
}
答案 2 :(得分:0)
测试已启用邮件的组的最安全方法是读取proxyAddresses并测试以“ smtp:”开头的任何条目。仅测试电子邮件字段是不够的。像这样扩展GroupPrincipal
public bool IsMailEnabled
{
get
{
var proxyAddresses = ExtensionGet("proxyAddresses");
if (proxyAddresses == null)
return false;
if (proxyAddresses.Length == 0)
return false;
try
{
List<string> proxyAddressesStringList = proxyAddresses.Cast<string>().ToList();
if (proxyAddressesStringList.Where(x => x.StartsWith("smtp:", StringComparison.InvariantCultureIgnoreCase)).Count() > 0)
return true;
else
return false;
}
catch
{
return false;
}
}
}