【问题标题】:UserPrincipal properties returning nullUserPrincipal 属性返回 null
【发布时间】:2015-04-24 07:49:16
【问题描述】:

最近我一直在研究一个非常大的应用程序的一小部分。在这一部分中,我需要使用 UserPrincipal 类从活动目录属性接收数据。

这适用于某些属性,例如 GivenName、Surname。但是当我试图获取像“name”这样的属性值时,我得到的是空值,我非常确定它们是用值而不是空值填充的。

首先我认为这是一个权限问题,所以我要求管理员授予我帐户的所有读取权限,他做到了,但我仍然无法读取所有属性。但是我可以使用 ActiveDirectoryExplorer 应用程序来阅读它们。

所以我的问题是;如果不是权限问题,有人知道这是什么原因吗?

提前致谢

【问题讨论】:

  • 调试它。当您获得一些用户时,请检查 UserPrincipal 的所有属性。如果填充了任何属性,例如 GivenName 或其他东西 - 比它被读取,所以如果某些属性为 null - 那么它真的是 nul

标签: c# active-directory


【解决方案1】:

执行以下代码,查看您的 AD 中所有可用的属性。您可能拼错了密钥或您的 AD 中不存在密钥

DirectorySearcher mySearcher = new System.DirectoryServices.DirectorySearcher(entry);

            foreach (System.DirectoryServices.SearchResult resEnt in mySearcher.FindAll())
            {
                try
                {
                    foreach (string property in resEnt.Properties.PropertyNames)
                    {
                        string value = resEnt.Properties[property][0].ToString();

                        Console.WriteLine(property + ":" + value);
                    }
                }
                catch (Exception)
                { }
            }

我的 Windows Server 2008 R2 AD 中的属性列表

objectClass=top;person;organizationalPerson;user
cn=x1
sn=LastName
c=PL
l=City
st=State
title=Job title
description=Description
postalCode=Zip
postOfficeBox=POBox
physicalDeliveryOfficeName=Office
telephoneNumber=123456779
givenName=FirstName
distinguishedName=CN=x1,CN=Users,DC=helpdesk,DC=wat,DC=edu
instanceType=4
whenCreated=2012-11-27 21:37:37
whenChanged=2012-12-11 21:33:51
displayName=DisplayName
uSNCreated=System.__ComObject
uSNChanged=System.__ComObject
co=Poland
department=Department
company=Company
streetAddress=Street
name=x1
objectGUID=System.Byte[]
userAccountControl=66048
badPwdCount=0
codePage=0
countryCode=616
badPasswordTime=System.__ComObject
lastLogoff=System.__ComObject
lastLogon=System.__ComObject
pwdLastSet=System.__ComObject
primaryGroupID=513
objectSid=System.Byte[]
accountExpires=System.__ComObject
logonCount=1
sAMAccountName=x1
sAMAccountType=805306368
userPrincipalName=x1@helpdesk.wat.edu
objectCategory=CN=Person,CN=Schema,CN=Configuration,DC=helpdesk,DC=wat,DC=edu
dSCorePropagationData=1601-01-01 00:00:00
lastLogonTimestamp=System.__ComObject
mail=mail@mail.com
homePhone=1236456654654659
mobile=800800800
nTSecurityDescriptor=System.__ComObject

【讨论】:

  • 真的很奇怪,当使用你的方法时,我实际上得到了每个属性值。但是,当使用 UserPrincipal 时,除了 SurName 和 GivenName 之外,我什么也得不到。不过谢谢!
【解决方案2】:

我遇到了同样的问题。我无法找出这些始终为空的实际原因。我的解决方法是将 UserPrincipal 转换为 DirectoryEntry。然后,您可以按名称调用属性。不理想,但可以。

请注意,这些属性中的实际缺失(空)值将导致异常,因此需要处理。

     //UserPrincipal user...
     DirectoryEntry d = (DirectoryEntry)user.GetUnderlyingObject();
     Console.WriteLine(d.Properties["GivenName"].Value.ToString() + d.Properties["sn"].Value.ToString());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    • 2013-05-10
    • 2019-07-17
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多