COM Unknown error (0x80005000) with PrincipalContext in the System.DirectoryServices.AccountManagement .NET Namespace

While working on a project recently, I ran into the very undescriptive COM Unknown error (0x80005000) trying to instantiate a PrincipalContext to use for some Active Directory management needs. To support a number of directory connection scenarios—including AD, AD LDS and the local machine SAM—our application allows admins to configure any set of relevant parameters used to create the PrincipalContext.

A subtle issue with the PrincipalContext constructor caused the COM Unknown Error and watch out for this when working with any of the System.DirectoryServices.AccountManagement classes. Merely passing blank values—empty strings—to the constructor for default parameters does not behave nicely and results in the COM Unknown Error. You must explicitly pass null for these values and then you will get the expected default behavior. You might use a pattern to check for empty string parameters in your wrapper class and set these values to null before constructing a PrincipalContext.

I suspect under the covers that the library authors include empty strings (String.Empty) as valid values and go ahead and pass these values to the underlying directory store. Of course, this causes an issue with most directories so just make sure to pass null.

Comments are closed.

Sign in
classic
Forgot password?
×
Sign up

(*) Required fields

I agree with OptimaSales Terms & Privacy Policy

×