首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用未在主体类中表示的AD属性

如何使用未在主体类中表示的AD属性
EN

Stack Overflow用户
提问于 2014-11-21 04:36:28
回答 2查看 1.5K关注 0票数 2

Principal类只有几个AD属性:

问题是我需要读取一个不在Principal类中的属性...

下面是我查询AD对象的方法:

代码语言:javascript
复制
// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain,ConfigurationManager.AppSettings["ADDomain"].ToString(), ConfigurationManager.AppSettings["ADUser"].ToString(), ConfigurationManager.AppSettings["ADPassword"].ToString());

// define a "query-by-example" principal - here, we search for all users
UserPrincipalEXT qbeUser = new UserPrincipalEXT(ctx);

// create your principal searcher passing in the QBE principal    
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

// find all matches
foreach (var found in srch.FindAll()) //FOUND represent the AD object
{
    ...
}

有没有办法扩展Principal类来获得更多的AD属性?

EN

回答 2

Stack Overflow用户

发布于 2014-11-21 04:47:27

如果您使用的是.NET 3.5或更高版本并使用System.DirectoryServices.AccountManagement (S.DS.AM)名称空间,则可以轻松地扩展现有的UserPrincipal类以获得更高级的属性,如Manager等。

请在这里阅读所有相关内容:

  • Managing Directory Security Principals in the .NET Framework 3.5
  • MSDN docs on System.DirectoryServices.AccountManagement

基本上,您只需定义一个基于UserPrincipal的派生类,然后定义所需的附加属性:

代码语言:javascript
复制
[DirectoryRdnPrefix("CN")]
[DirectoryObjectClass("Person")]
public class UserPrincipalEx : UserPrincipal
{
    // Inplement the constructor using the base class constructor. 
    public UserPrincipalEx(PrincipalContext context) : base(context)
    { }

    // Implement the constructor with initialization parameters.    
    public UserPrincipalEx(PrincipalContext context,
                         string samAccountName,
                         string password,
                         bool enabled) : base(context, samAccountName, password, enabled)
    {} 

    // Create the "Department" property.    
    [DirectoryProperty("department")]
    public string Department
    {
        get
        {
            if (ExtensionGet("department").Length != 1)
                return string.Empty;

            return (string)ExtensionGet("department")[0];
        }
        set { ExtensionSet("department", value); }
    }

    // Create the "Manager" property.    
    [DirectoryProperty("manager")]
    public string Manager
    {
        get
        {
            if (ExtensionGet("manager").Length != 1)
                return string.Empty;

            return (string)ExtensionGet("manager")[0];
        }
        set { ExtensionSet("manager", value); }
    }
}

现在,您可以在代码中使用UserPrincipalEx的“扩展”版本:

代码语言:javascript
复制
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
    // Search the directory for the new object. 
    UserPrincipalEx inetPerson = UserPrincipalEx.FindByIdentity(ctx, IdentityType.SamAccountName, "someuser");

    // you can easily access the Manager or Department now
    string department = inetPerson.Department;
    string manager = inetPerson.Manager;
}        
票数 4
EN

Stack Overflow用户

发布于 2014-11-21 04:41:42

您可以使用GetUnderlyingObject()访问其他属性:

代码语言:javascript
复制
if (found.GetUnderlyingObjectType() == typeof(DirectoryEntry))
{
    DirectoryEntry de = (DirectoryEntry)principal.GetUnderlyingObject();
    // Use de.Properties to access additional information
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27049074

复制
相关文章

相似问题

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