c# - 扩展 UserPrincipal 类

标签 c# active-directory userprincipal groupprincipal principalsearcher

我扩展了 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();

因此,我的新任务和我当前的问题是:对于在 ActiveDirectory 中搜索到的组,有必要使用扩展类获取用户列表。

GroupPrincipal group = (GroupPrincipal)collection.FirstOrDefault();

foreach (Principal pRes in group.GetMembers())
{
   //This doesnt work of course.
   // return null value.
   UserPrincipalExt user = pRes as UserPrincipalExt;
}

我怎样才能实现目标?

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

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"
);

查看此 Link

关于c# - 扩展 UserPrincipal 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24798037/

相关文章:

c# - Fluent NHibernate 或 NHibernate for Linq?

c# - LINQ to SQL 查询返回重复项

c# - Visual Studio 不支持的 Android 项目类型

c# - 扩展用户主体; FindByIdentity() 失败

c# - 获取给定 UserPrincipal 的组列表

c# - 将表而不是范围定义为数据透视表 'cacheSource'

powershell - Active Directory "-approx"过滤器运算符如何工作?

powershell - 在Get-ADComputer中排序

c# - LDAP - 检索所有属性/值的列表?

java - Active Directory userPrincipleName 与用户登录名不同