2

I have an application in .net core. the application was running fine, but the error started coming after I installed

dotnet add package Microsoft.AspNetCore.Authentication.Negotiate --version 6.0.14

Once I installed this package and put this line of code in startup.cs file:

  services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
           .AddNegotiate();
        services.AddAuthorization(options =>
        {
            options.FallbackPolicy = options.DefaultPolicy;
        });

I started getting this error:

System.IO.FileNotFoundException: 'Could not load file or assembly 'System.DirectoryServices.Protocols, Version=6.0.0.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.'

I installed this package when I saw the above error:

dotnet add package System.DirectoryServices --version 7.0.0

I am still getting this error. The error is coming in program.cs file. below is the screen shot of the error:

enter image description here

unfortunately, I dont see version 6.0.0.1 for System.directoryServices.protocols in nuget. Not sure, why the application is looking for 6.0.0.1 version when I have 7.0.0 version installed. Below is packages installed on my application:

enter image description here

I tried putting this in my web.config file:

<configuration>
   <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.DirectoryServices.Protocols" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.1" newVersion="7.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
</configuration>
Anjali
  • 2,540
  • 7
  • 37
  • 77
  • Try to update the version of the `System.DirectoryServices.Protocols` package to a version that is compatible with the other packages in your application. `Update-Package System.DirectoryServices.Protocols` – Pranav Bilurkar Mar 02 '23 at 17:52
  • The error is asking for a lower version of System.DirectoryServices.Protocols. If I upgrade, wouldn't it throw the error again – Anjali Mar 02 '23 at 17:56
  • Alternatively, have you tried adding a binding redirect to your project's `app.config` or `web.config` file to redirect requests for version `6.0.0.1` to the version that is available in your application – Pranav Bilurkar Mar 02 '23 at 18:00
  • I put the binding redirect in my web.config file, it still gives an error – Anjali Mar 02 '23 at 18:53
  • I noticed the assembly version of System.DirectoryServices.Protocols of the package 7.0.0 is 4.0.0.0. So the binding redirect might have to point to 4.0.0.0 to make this work. I am having the same issue with dotnet 6 where there are no binding redirects and I don't know how to solve this – rominator007 Apr 25 '23 at 06:27
  • oh having the same issue after all. look here. your binding redirects will not work in .net 6 any more https://stackoverflow.com/a/46120907/4997872 – rominator007 Apr 25 '23 at 06:29

1 Answers1

3

Update: This is a bug in System.DirectoryServices.Protocols 7.0.0 and Microsoft.AspNetCore.Authentication.Negotiate that is already fixed and will be shipped with the next releases of the packages. See the response to the issue here https://github.com/dotnet/runtime/issues/85296#issuecomment-1522092739

In the meantime use my work around of using the older versions of the packages.


Previous analysis (before update)

I did not solve but work around this issue and I will share my insights with you. Maybe someone finds the root cause of this based on my findings?

I had the same error when running a published dotnet 6 exe. The problem occurred when upgrading Microsoft.AspNetCore.Authentication.Negotiate to 6.0.16 and System.DirectoryServices.Protocols from 6.0.1 to 7.0.0. Running the exe from within Visual Studio with F5 works and I do not have an explanation for that. We are using <SelfContained>false</SelfContained> in our publish profile.

The difference to your solution might be that we need to reference System.DirectoryServices.Protocols directly in another project (not the exe directly) since we programmatically access an LDAP server in the same exe too.

I noticed the assembly version of System.DirectoryServices.Protocols of the package 7.0.0 is 4.0.0.0 whereas in package version 6.0.1 the assembly version is 6.0.0.1 (See screenshots below). So with a newer package version you get a lower assembly version. This is odd and I don't think that's best practice. I do think, that this might throw the dependency management of dotnet during the publish step off but I did not find any information or evidence for that. I filed an issue for System.DirectoryServices because of the assembly version: https://github.com/dotnet/runtime/issues/85296

Side note: The assembly version is NOT shown in the properties dialog of windows explorer. The safest way to get the assembly version is via https://learn.microsoft.com/en-us/dotnet/framework/tools/ildasm-exe-il-disassembler.

enter image description here

enter image description here

So your binding redirect would have to point to 4.0.0.0 to make this work but alas there are no binding redirects in dotnet 6 any more (https://stackoverflow.com/a/46120907/4997872).

Based on the further information I found online (https://www.reddit.com/r/csharp/comments/112h640/could_not_load_file_or_assembly/ https://learn.microsoft.com/en-us/answers/questions/1185993/system-io-filenotfoundexception-could-not-load-fil) the only working solution at the moment seems to be to stay with System.DirectoryServices.Protocols 6.0.1 and reference that in your application directly. At least that is what worked for us.

It seems either that something is broken in 7.0.0 of System.DirectoryServices.Protocols causing the conflict or at least Microsoft.AspNetCore.Authentication.Negotiate 6.0.x is not working with System.DirectoryServices.Protocols 7.0.0.

rominator007
  • 1,553
  • 1
  • 11
  • 22