首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自AccountManagement.Principal的基于搜索器组属性的Active

来自AccountManagement.Principal的基于搜索器组属性的Active
EN

Stack Overflow用户
提问于 2013-08-01 12:26:45
回答 1查看 498关注 0票数 0

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

我试图从Active Directory获得所有组和属性,如下所示。但我还有其他属性,因为键值是System.DirectoryServices.SearchResult,

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

代码语言:javascript
复制
// 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())
{
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-01 14:03:28

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

代码语言:javascript
复制
 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");
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17994030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档