“Properties”和“GetProperties”不适用于 UserPrincipal,无法获取“Department”和其他值

问题描述

我正在尝试获取 AD 用户的“部门”值,我有一个 UserPrincipal 对象,我使用以下内容。

public ADUser getADUser(string sid)
    {
        ADUser ADUser = new ADUser();

        string container = "OU=Users,OU=comp,DC=domain,DC=domaindomain";
        using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain,"domain",container))
        {
            UserPrincipal userPrinciple = new UserPrincipal(ctx) { Enabled = true };
            PrincipalSearcher principalSearcher = new PrincipalSearcher(userPrinciple);
            ((DirectorySearcher)principalSearcher.GetUnderlyingSearcher()).SearchScope = SearchScope.OneLevel;

            UserPrincipal domainUser = UserPrincipal.FindByIdentity(ctx,IdentityType.Sid,(sid));                

            ADUser = new ADUser()
            {
                SID = domainUser.Sid.ToString(),GUID = domainUser.Guid.ToString(),Description = domainUser.Description,DisplayName = domainUser.DisplayName,EmailAddress = domainUser.EmailAddress,VoiceTelephoneNumber = domainUser.VoiceTelephoneNumber,Enabled = domainUser.Enabled,permissions = getADUserPermissions(sid)
            };
               
        } //using

        return ADUser;
    }

但是当我尝试使用以下任一方法获取“部门”值时...

domainUser.GetProperty("department");

domainUser.Properties["department"].Value;

我收到错误消息,“'UserPrincipal' 不包含“GetProperty”或“Properties”的定义。

我发现“部门”在 UserPrincipal 中底层对象的属性中,所以我尝试...

domainUser.GetUnderlyingObject();

哪个获取底层对象,并且在调试时我可以在其属性中看到“部门”,但是没有访问属性的功能(至少我的 .net Core 3.1 版本似乎没有让我使用的功能).

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)