扩展UserPrincipal类 [英] Extend UserPrincipal class

查看:88
本文介绍了扩展UserPrincipal类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I do the extension of the UserPrincipal class to retrieve some missing properties that i need:

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

然后,我可以像这样轻松地进行搜索:

Then, i can search easily like this:

 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中搜索到的组,当然必须使用扩展类来获取用户列表.

So, my new task, and my current problem, is this: for the searched Group in ActiveDirectory, it is necessary get the list of the users, using the extended class, of course.

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

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

我如何实现目标?

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

As workaround i have made a function to retrieve the properties anyway :

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方法.

Override the FindByIdentity method in your extended class.

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方法搜索

Then search using the extended class FindByIdentity method

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

请参见 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆