Trying to use System.DirectoryServices.AccountManagement in a .Net 5.0 Project

667 views Asked by At

I am trying to create a new PrincipalContext() variable in a C# .NET project.

I have added a Project Reference to C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.8\System.DirectoryServices.AccountManagement.dll.

I have my Output type set to 'Windows Application' and my Target framework set to .NET 5.0.

In my object, I have using System.DirectoryServices.AccountManagement;

When I add the following code:

PrincipalContext pc = new PrincipalContext(ContextType.Domain, iDomainName);

and try to execute, I get the following run time error:

System.BadImageFormatException: 'Could not load file or assembly 'System.DirectoryServices.AccountManagement, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (0x80131058)'

Any ideas on how to proceed to resolve this?

I have tried to add a reference to the .NET 4.0, .NET 4.7.2 and .NET 4.8 versions of AccountManagement. I have tried to follow the example code for Loading an Assembly using ReflectionOnly but am getting lost in what to use for type, method and parameters.

What I expect is that the PrincipalContext variable PC is created and I can use that to reference FindByIdentity and ValidateCredentials.

0

There are 0 answers