Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# UserPrincipal属性返回null_C#_Active Directory - Fatal编程技术网

C# UserPrincipal属性返回null

C# UserPrincipal属性返回null,c#,active-directory,C#,Active Directory,最近我一直在处理一个非常大的应用程序的一小部分。在本部分中,我需要使用UserPrincipal类从active directory属性接收数据 这适用于某些属性,例如GivenName、姓氏。但是当我试图获取像'name'这样的属性值时,我得到的是空值,我非常确定它们是用值填充的,而不是空值 首先,我认为这是一个权限问题,所以我要求管理员为我的帐户授予所有阅读权限,他这样做了,但我仍然无法读取所有属性。但我可以使用ActiveDirectoryExplorer应用程序读取所有这些内容 所以我的

最近我一直在处理一个非常大的应用程序的一小部分。在本部分中,我需要使用UserPrincipal类从active directory属性接收数据

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

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

所以我的问题是,;当这不是许可问题时,有人知道这是什么原因吗


提前感谢

执行以下代码,查看您的广告中的所有可用属性。有一个更改,您可能拼错了密钥,或者您的广告中不存在密钥

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强制转换为DirectoryEntry。然后可以按名称调用属性。虽然不理想,但它确实有效

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

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

调试它。当您获得一些用户时,请检查UserPrincipal的所有属性。如果有任何属性像GivenName或其他什么东西一样被填充,那么如果某个属性为null,那么它确实是非常奇怪的,当使用你的方法时,我实际上得到了每个属性值。但是,当使用UserPrincipal时,除了姓氏和GivenName,我什么也得不到。谢谢你!