Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.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# 如何使用主体类中未表示的AD属性_C#_Active Directory - Fatal编程技术网

C# 如何使用主体类中未表示的AD属性

C# 如何使用主体类中未表示的AD属性,c#,active-directory,C#,Active Directory,Principal类只有几个广告属性: 问题是我需要读取一个不在Principal类中的属性 下面是我查询广告对象的方式: // create your domain context PrincipalContext ctx = new PrincipalContext(ContextType.Domain,ConfigurationManager.AppSettings["ADDomain"].ToString(), ConfigurationManager.AppSettings["ADU

Principal
类只有几个广告属性:

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

下面是我查询广告对象的方式:

// 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
{
    ...
}

是否有方法扩展
主体
类以获取更多广告属性?

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

if (found.GetUnderlyingObjectType() == typeof(DirectoryEntry))
{
    DirectoryEntry de = (DirectoryEntry)principal.GetUnderlyingObject();
    // Use de.Properties to access additional information
}

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

请在此处阅读所有相关内容:

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

[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
的“扩展”版本:

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

回答得很好。谢谢@marc_s我正在使用.NET4.7,但仍然不能像manager那样获取属性