0

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.

Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84
  • Those things should be in a '.NET Platform Extensions' so install the 'Microsoft.Windows.Compatibility' nuget package for NET 5. Don't try to reference old incompatible assemblies. – Ralf Apr 24 '23 at 13:02
  • https://www.nuget.org/packages/System.DirectoryServices.AccountManagement/5.0.0 – Hans Passant Apr 24 '23 at 14:46
  • Ding ding ding...we have a winner. Thank you so very much. – Mike Weber Apr 24 '23 at 15:22

0 Answers0