Bug in ActiveDirectory access of .NET Framework

  • Thread starter Thread starter Marco Herrn
  • Start date Start date
M

Marco Herrn

Hi,

I have an application in C# that manages users, shares and printers in
the AD. This application sometimes runs on an error accessing the AD.
This is not really reproducable, it happens often, but not always.
Sometimes it runs on an error and later goes over the same code again
and works. The error message is always a NullReferenceException on
System.DirectoryServices.Interop.IAds.GetEx, for example:

at System.DirectoryServices.Interop.IAds.GetEx(String bstrName)
at System.DirectoryServices.PropertyValueCollection.PopulateList()
at System.DirectoryServices.PropertyCollection.get_Item(String
propertyName)
at amt.user.UserManagement.getUserByDirectoryEntry(DirectoryEntry
ADEntry) in c:\inetpub\wwwroot\amt\sharp\user\UserManagement.cs:line 2098
at amt.user.UserManagement.getUserByProperty(String Property, String
domain) in c:\inetpub\wwwroot\amt\sharp\user\UserManagement.cs:line 1900
at amt.user.UserManagement.getUserBySAM(String SAM, String domain)
in c:\inetpub\wwwroot\amt\sharp\user\UserManagement.cs:line 576
at replication.RootDSEReplicator.checkUserReplicationStatus(AddEntry
entry) in
c:\inetpub\wwwroot\amt\sharp\replication\RootDSEReplicator.cs:line 284
at replication.RootDSEReplicator.checkReplicationStatus(Object
state) in
c:\inetpub\wwwroot\amt\sharp\replication\RootDSEReplicator.cs:line 244

I haven't seen any pattern when this happens. The NulRef obviously
doesn't happen in my code. It seems to happen in one case on this action:

if(ADEntry.Properties["distinguishedName"].Value != null){
newUser.DN= ADEntry.Properties["distinguishedName"].Value.ToString();
}

Any ideas what can cause this? What surprises me is, that the NullRef
isn't thrown by my code, but from inside IAds.

Regards
Marco
 
I haven't seen any pattern when this happens. The NulRef obviously
doesn't happen in my code. It seems to happen in one case on this action:

if(ADEntry.Properties["distinguishedName"].Value != null){
newUser.DN= ADEntry.Properties["distinguishedName"].Value.ToString();

The NullRef DOES happen in your code !!!

If your entry does NOT contain a value for "distinguishedName", then
the ADEntry.Properties["distinguishedName"] will be NULL, and
trying to access its .Value property will result in that error.

You need to check like this:

if(ADEntry.Properties.Contains("distinguishedName").....
{ // do something, like accessing the .Value property }

or

if(ADEntry.Properties["distinguishedName"] != null)
{ // do something, like accessing the .Value property }

Marc
================================================================
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch
 
Back
Top