1
votes

Recently I've been working on a small part of a very large application. In this part I need to receive data from active directory properties using the UserPrincipal class.

This works fine for some properties i.e GivenName, Surname. But when I'm trying to get the values of properties like 'name' I'm getting null values, and I'm very very sure they are filled with values and not null.

First I thought this was a permission issue, so i asked the admin to give all reading permissions to my account, he did, but still I can't read all properties. BUT I can read them all using the ActiveDirectoryExplorer application.

So my question is; does somebody know what's the cause of this when it's not a permission issue.

Thanks in advance

2
Debug it. When You get some user check all properties of UserPrincipal. If any property is filled in like GivenName or something - than it was read so if some property is null - then it is really nulMajkeloDev

2 Answers

1
votes

Execute the below code and see what are all the property's available in your AD. There is a change that you might have misspell the key or the key does not exist in your 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)
                { }
            }

List of properties in my 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
[email protected]
objectCategory=CN=Person,CN=Schema,CN=Configuration,DC=helpdesk,DC=wat,DC=edu
dSCorePropagationData=1601-01-01 00:00:00
lastLogonTimestamp=System.__ComObject
[email protected]
homePhone=1236456654654659
mobile=800800800
nTSecurityDescriptor=System.__ComObject
1
votes

I had the same problem. I was not able to find out an actual reason why these are always null. My workaround was to cast the UserPrincipal to DirectoryEntry. You can then call properties by name. Not ideal, but it works.

Note that actual missing(null) values in those properties will cause an exception, so that needs to be handled.

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