首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >扩展UserPrincipal类

扩展UserPrincipal类
EN

Stack Overflow用户
提问于 2014-07-17 16:04:03
回答 2查看 3.9K关注 0票数 6

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

代码语言:javascript
复制
[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);
        }
    }
}

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

代码语言:javascript
复制
 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中的搜索组,需要获取用户列表,当然是使用扩展类。

代码语言:javascript
复制
GroupPrincipal group = (GroupPrincipal)collection.FirstOrDefault();

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

我怎样才能达到这个目标?

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

代码语言:javascript
复制
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;
    }

提前谢谢你。

EN

回答 2

Stack Overflow用户

发布于 2018-02-08 01:47:44

重写扩展类中的FindByIdentity方法。

代码语言:javascript
复制
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方法进行搜索

代码语言:javascript
复制
var user = User.FindByIdentity(
    DomainContext,
    "name"
);

查看此Link

票数 2
EN

Stack Overflow用户

发布于 2019-02-06 02:17:57

dblock247给出了正确答案

代码语言:javascript
复制
    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);
    }

你可以用这种方式得到本金

代码语言:javascript
复制
  UserPrincipalExt oUserPrincipal = UserPrincipalExt.FindByIdentity(oPrincipalContext, IdentityType.UserPrincipalName, userName);

像这样的属性:

代码语言:javascript
复制
Console.WriteLine(oUserPrincipal.Company);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24798037

复制
相关文章

相似问题

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