C# 扩展UserPrincipal类

C# 扩展UserPrincipal类,c#,active-directory,userprincipal,groupprincipal,principalsearcher,C#,Active Directory,Userprincipal,Groupprincipal,Principalsearcher,我对UserPrincipal类进行扩展,以检索我需要的一些缺少的属性: [DirectoryObjectClass("user")] [DirectoryRdnPrefix("CN")] class UserPrincipalExt : UserPrincipal { public UserPrincipalExt(PrincipalContext context) : base(context) { } [DirectoryProperty(

我对UserPrincipal类进行扩展,以检索我需要的一些缺少的属性:

[DirectoryObjectClass("user")]
[DirectoryRdnPrefix("CN")]
class UserPrincipalExt : UserPrincipal
{
    public UserPrincipalExt(PrincipalContext context)
        : base(context)
    {
    }

    [DirectoryProperty("department")]
    public string Department
    {
        get
        {
            if (ExtensionGet("department").Length != 1)
                return null;
            return (string)ExtensionGet("department")[0];
        }
        set 
        { 
            this.ExtensionSet("department", value); 
        }
    }

    [DirectoryProperty("company")]
    public string Company
    {
        get
        {
            if (ExtensionGet("company").Length != 1)
                return null;
            return (string)ExtensionGet("company")[0];
        }
        set
        {
            this.ExtensionSet("company", value);
        }
    }

    [DirectoryProperty("c")]
    public string CountryAbbreviation
    {
        get
        {
            if (ExtensionGet("c").Length != 1)
                return null;
            return (string)ExtensionGet("c")[0];
        }
        set
        {
            this.ExtensionSet("c", value);
        }
    }
}
然后,我可以像这样轻松搜索:

 PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, myDomain);
 UserPrincipalExt userExt = new UserPrincipalExt(principalContext);
 PrincipalSearcher searcher = new PrincipalSearcher(userExt);

 userExt.GivenName = "blabla";
 userExt.EmailAddress ="text here";

 PrincipalSearchResult<Principal> searchTmp = null;

 searcher.QueryFilter = userExt;
 searchTmp = searcher.FindAll();
Console.WriteLine(oUserPrincipal.Company);
我怎样才能达到目标

作为解决方法,我创建了一个函数来检索属性:

private string GetExtendedProperty(Principal principal, string propertyTo)
    {
        string property = "";

        try
        {
            DirectoryEntry directoryEntry = principal.GetUnderlyingObject() as DirectoryEntry;

            if (directoryEntry.Properties.Contains(propertyTo))
            {
                property = directoryEntry.Properties[propertyTo].Value.ToString();
            }
            else
            {
                property = "";
            }
        }
        catch (Exception ex)
        {
            Logger.ScriviLog(4, this.GetType().Name, MethodBase.GetCurrentMethod().Name, ex.Message);
        }

        return property;
    }

提前感谢。

在扩展类中重写FindByIdentity方法

public new static User FindByIdentity(PrincipalContext context, string identityValue)
{
    return (User)FindByIdentityWithType(context, typeof(User), identityValue);
}

public new static User FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)
{
    return (User)FindByIdentityWithType(context, typeof(User), identityType, identityValue);
}
然后使用扩展类FindByIdentity方法进行搜索

var user = User.FindByIdentity(
    DomainContext,
    "name"
);

见此

dblock247的答案正确

    public new static UserPrincipalExt FindByIdentity(PrincipalContext context, string identityValue)
    {
        return (UserPrincipalExt)FindByIdentityWithType(context, typeof(UserPrincipalExt), identityValue);
    }

    public new static UserPrincipalExt FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)
    {
        return (UserPrincipalExt)FindByIdentityWithType(context, typeof(UserPrincipalExt), identityType, identityValue);
    }
这样你就可以拿到本金了

  UserPrincipalExt oUserPrincipal = UserPrincipalExt.FindByIdentity(oPrincipalContext, IdentityType.UserPrincipalName, userName);
以及像这样的属性:

 PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, myDomain);
 UserPrincipalExt userExt = new UserPrincipalExt(principalContext);
 PrincipalSearcher searcher = new PrincipalSearcher(userExt);

 userExt.GivenName = "blabla";
 userExt.EmailAddress ="text here";

 PrincipalSearchResult<Principal> searchTmp = null;

 searcher.QueryFilter = userExt;
 searchTmp = searcher.FindAll();
Console.WriteLine(oUserPrincipal.Company);