Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 确定广告安全组的电子邮件地址_C#_Active Directory_Directoryservices - Fatal编程技术网

C# 确定广告安全组的电子邮件地址

C# 确定广告安全组的电子邮件地址,c#,active-directory,directoryservices,C#,Active Directory,Directoryservices,在工作中的AD中,我们有一些启用邮件的安全组。我正在使用System.DirectoryServices.AccountManagement命名空间,如下所示: List<GroupPrincipal> result = new List<GroupPrincipal>(); using (PrincipalContext domain = new PrincipalContext(ContextType.Domai

在工作中的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);                        
                }                
            }
        }
列表结果=新列表();
使用(PrincipalContext域=新PrincipalContext(ContextType.domain,userinfo[0]))
使用(UserPrincipal user=UserPrincipal.FindByIdentity(域、用户名))
{
如果(用户!=null)
{
PrincipalSearchResult groups=user.GetAuthorizationGroups();
int totalGroupCounter=0;
StringBuilder输出=新的StringBuilder();
List securityGroups=new List();
List distributionGroups=新列表();
foreach(组中的主要组)
{
totalGroupCounter++;
if(((GroupPrincipal)group.IsSecurityGroup.Value)
添加((GroupPrincipal)组);
其他的
distributionGroups.Add((GroupPrincipal)组);
}                
}
}

有了这个信息,什么是正确的方法找到组的电子邮件地址?

< P>我认为MARCICS是ActiveDirectory主题的专家,但是,我也有一个安全组,它有一个与之相关的电子邮件地址。下面是我如何从中获取电子邮件的:

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;
    }
}

AccountManagement库限制您可以访问的属性。如果要获取组的电子邮件属性,则需要将其强制转换回对象


测试启用邮件的组最安全的方法是读取代理地址并测试以“smtp:”开头的任何条目。仅测试电子邮件字段是不够的。扩展GroupPrincipal-like

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;
                }
            }
        }
public bool已启用
{
得到
{
var proxyAddresses=ExtensionGet(“proxyAddresses”);
if(proxyAddresses==null)
返回false;
if(proxyAddresses.Length==0)
返回false;
尝试
{
List proxyAddressesStringList=proxyAddresses.Cast().ToList();
if(proxyAddresseStringList.Where(x=>x.StartsWith(“smtp:,StringComparison.InvariantCultureIgnoreCase)).Count()>0)
返回true;
其他的
返回false;
}
抓住
{
返回false;
}
}
}

您曾经解决过这个问题吗?对于像我这样多年后偶然发现这个问题的人,请注意,您需要添加对System.DirectoryServices的引用
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;
                }
            }
        }