4

ここで機能している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);                        
                }                
            }
        }

この情報で武装して、グループの電子メールアドレスを見つける正しい方法は何ですか?

4

3 に答える 3

12

AccountManagementライブラリは、アクセスできるプロパティを制限します。DirectoryEntryグループのemailプロパティを取得する場合は、それをオブジェクトにキャストする必要があります。

PropertyValueCollection email = ((DirectoryEntry)group.GetUnderlyingObject()).Properties["mail"];
if (email.Value != null)
{
    // Do something with email property
}
于 2013-03-19T16:41:42.830 に答える
0

marc_sは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;
    }
}
于 2013-02-05T15:44:42.957 に答える
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;
                }
            }
        }
于 2019-05-15T08:36:48.553 に答える