0

我在 .NET 4.5 中使用 System.DirectoryServices.AccountManagement。

我正在尝试从 Active Directory 中获取所有组和属性,如下所示。但是我有其他属性被定义为 Key Value is System.DirectoryServices.SearchResult ,

如何从 System.DirectoryServices.AccountManagement.Principal 获取 System.DirectoryServices.SearchResult 下的所有属性

// create your domain context
var ctx = new PrincipalContext(ContextType.Domain, _domain, _userName, _password);

// define a "query-by-example" principal - here, we search for a GroupPrincipal 
 var groupPrincipal = new GroupPrincipal(ctx);
// create your principal searcher passing in the QBE principal    
var searcher = new PrincipalSearcher(groupPrincipal);
foreach (var found in searcher.FindAll())
{
}
4

1 回答 1

0

我已经用下面的扩展方法解决了这个问题,但是如果有任何有效的方法可以做同样的工作,请建议我。

 public static class AccountManagementExtensions
    {
        public static String GetProperty(this Principal principal, String property)
        {
            var directoryEntry = principal.GetUnderlyingObject() as DirectoryEntry;
            return directoryEntry != null && directoryEntry.Properties.Contains(property)
                       ? directoryEntry.Properties[property].Value.ToString()
                       : String.Empty;
        }

        public static String GetCompany(this Principal principal)
        {
            return principal.GetProperty("company");
        }

        public static String GetDepartment(this Principal principal)
        {
            return principal.GetProperty("department");
        }
    }
于 2013-08-01T14:03:28.003 回答