locked
Keep getting "The located assembly's manifest definition does not match the assembly reference." when starting a new Web API project RRS feed

  • Question

  • User-900017823 posted

    In the last few months I've started 3 new Web API projects. Each of these are meant to hit against an ADFS server we have in-house for development. Every time I do, before I start programming anything I build and run it to see if it works. It always fails we ah error that says:

    "Could not load file or assembly 'Microsoft.IdentityModel.Protocols.WsFederation, Version=5.2.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)"

    I found an post on Stack Overflow on this, which I thought would handle it, but now it doesn't. It's very strange how just starting a new project, putting in other the information required for ADFS, then trying to run it consistently fails. And it bothers me that what did work a few months ago (I asked a question in that post back in June now doesn't work at all. My guess is that something is changing when new updates to Visual Studio 2017 come out, which breaks compatibility with whatever assemblies is depending upon 'Microsoft.IdentityModel.Protocols.WsFederation.

    Anyway, here's the full text of the error I'm getting:

    System.IO.FileLoadException
    HResult=0x80131040
    Message=Could not load file or assembly 'Microsoft.IdentityModel.Protocols.WsFederation, Version=5.2.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
    Source=Microsoft.Owin.Security.ActiveDirectory
    StackTrace:
    at Microsoft.Owin.Security.ActiveDirectory.WsFedMetadataRetriever.GetSigningKeys(String metadataEndpoint, TimeSpan backchannelTimeout, HttpMessageHandler backchannelHttpHandler)
    at Microsoft.Owin.Security.ActiveDirectory.WsFedCachingSecurityKeyProvider.RetrieveMetadata()
    at Microsoft.Owin.Security.ActiveDirectory.WsFedCachingSecurityKeyProvider..ctor(String metadataEndpoint, ICertificateValidator backchannelCertificateValidator, TimeSpan backchannelTimeout, HttpMessageHandler backchannelHttpHandler)
    at Owin.ActiveDirectoryFederationServicesBearerAuthenticationExtensions.UseActiveDirectoryFederationServicesBearerAuthentication(IAppBuilder app, ActiveDirectoryFederationServicesBearerAuthenticationOptions options)
    at MiniCoreWebAPI.Startup.ConfigureAuth(IAppBuilder app) in D:\Src\MiniCoreWebAPI\MiniCoreWebAPI\App_Start\Startup.Auth.cs:line 18
    at MiniCoreWebAPI.Startup.Configuration(IAppBuilder app) in D:\Src\MiniCoreWebAPI\MiniCoreWebAPI\Startup.cs:line 9

    Inner Exception 1:
    FileLoadException: Could not load file or assembly 'Microsoft.IdentityModel.Protocols.WsFederation, Version=5.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

    And here's what I believe is the relevant section from the Web.Config file:

    <runtime>
    	<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    	  <dependentAssembly>
    		<assemblyIdentity name="Microsoft.IdentityModel.Protocols.WsFederation" publicKeyToken="31BF3856AD364E35" culture="neutral" />
    		<bindingRedirect oldVersion="0.0.0.0-5.2.1.0" newVersion="5.2.1.0" />
    	  </dependentAssembly>
    	  <dependentAssembly>
    		<assemblyIdentity name="Microsoft.IdentityModel.Tokens.Saml" publicKeyToken="31BF3856AD364E35" culture="neutral" />
    		<bindingRedirect oldVersion="0.0.0.0-5.2.1.0" newVersion="5.2.1.0" />
    	  </dependentAssembly>
    	  <dependentAssembly>
    		<assemblyIdentity name="Microsoft.IdentityModel.Tokens" publicKeyToken="31bf3856ad364e35" />
    		<bindingRedirect oldVersion="0.0.0.0-5.2.4.0" newVersion="5.2.4.0" />
    	  </dependentAssembly>
    	  <dependentAssembly>
    		<assemblyIdentity name="Microsoft.IdentityModel.Protocols.OpenIdConnect" publicKeyToken="31bf3856ad364e35" />
    		<bindingRedirect oldVersion="0.0.0.0-5.2.1.0" newVersion="5.2.1.0" />
    	  </dependentAssembly>
    	  <dependentAssembly>
    		<assemblyIdentity name="Microsoft.IdentityModel.Protocols" publicKeyToken="31bf3856ad364e35" />
    		<bindingRedirect oldVersion="0.0.0.0-5.2.1.0" newVersion="5.2.1.0" />
    	  </dependentAssembly>
    	  <dependentAssembly>
    		<assemblyIdentity name="System.IdentityModel.Tokens.Jwt" publicKeyToken="31bf3856ad364e35" />
    		<bindingRedirect oldVersion="0.0.0.0-5.2.1.0" newVersion="5.2.1.0" />
    	  </dependentAssembly>
    	  <dependentAssembly>
    		<assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" />
    		<bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
    	  </dependentAssembly>
    	  <dependentAssembly>
    		<assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" />
    		<bindingRedirect oldVersion="0.0.0.0-4.0.2.1" newVersion="4.0.2.1" />
    	  </dependentAssembly>
    	  <dependentAssembly>
    		<assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
    		<bindingRedirect oldVersion="0.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
    	  </dependentAssembly>
    	  <dependentAssembly>
    		<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
    		<bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
    	  </dependentAssembly>
    	  <dependentAssembly>
    		<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
    		<bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
    	  </dependentAssembly>
    	  <dependentAssembly>
    		<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
    		<bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
    	  </dependentAssembly>
    	  <dependentAssembly>
    		<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    		<bindingRedirect oldVersion="0.0.0.0-5.2.4.0" newVersion="5.2.4.0" />
    	  </dependentAssembly>
    	  <dependentAssembly>
    		<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
    		<bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
    	  </dependentAssembly>
    	  <dependentAssembly>
    		<assemblyIdentity name="Microsoft.IdentityModel.Xml" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    		<bindingRedirect oldVersion="0.0.0.0-5.2.1.0" newVersion="5.2.1.0" />
    	  </dependentAssembly>
    	  <dependentAssembly>
    		<assemblyIdentity name="Microsoft.IdentityModel.Logging" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    		<bindingRedirect oldVersion="0.0.0.0-5.2.4.0" newVersion="5.2.4.0" />
    	  </dependentAssembly>
    	  <dependentAssembly>
    		<assemblyIdentity name="Microsoft.IdentityModel.Protocols.WsFederation" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    		<bindingRedirect oldVersion="0.0.0.0-5.2.4.0" newVersion="5.2.4.0" />
    	  </dependentAssembly>
    	</assemblyBinding>
    </runtime>
    

    Note that there are 2 sections in the above related to Microsoft.IdentityModel.Protocols.WsFederation. I'm not entirely sure how come that is, but I'm thinking this may now have something to do with my problem. When I first got the error I tried install the NuGet package Microsoft.IdentityModel.Protocols.WsFederation for version 5.2.0.0. But that seemed only to cause another problem, or at least not address the issue I was having. So I went to remove it, but that didn't work. However the Manage NuGet Packages... in VS 2017 did allow me to upgrade from 5.2.0.0 to 5.2.4.0, so I went ahead and did that. I think that might be why there's that second entry in the Web.Config file. Not entirely sure I'm correct about that. If I am, why didn't the upgrade remove the older version? At the end of the day, I need to know why this is failing again and how to remedy it. And for that matter, I'd like to know how to stop getting this every time I create a new Web API?

    Monday, July 16, 2018 8:56 PM

All replies

  • User36583972 posted


    Hi DoctorWho,

    . My guess is that something is changing when new updates to Visual Studio 2017 come out, which breaks compatibility with whatever assemblies is depending upon 'Microsoft.IdentityModel.Protocols.WsFederation.

    Is your previous version running properly?

    if you use the latest Visual Studio 2017, you may can try to update the Microsoft.IdentityModel.Protocols.WsFederation to Version 5.2.4.


    Best Regards,

    Yong Lu

    Tuesday, July 17, 2018 9:44 AM
  • User475983607 posted

    Are we to understand that you create a new API project and get this error without adding any NuGet packages or modifying the project in any way?  Which template are you using and what framework are you targeting?

    Tuesday, July 17, 2018 2:11 PM
  • User-900017823 posted

    Are we to understand that you create a new API project and get this error without adding any NuGet packages or modifying the project in any way?  Which template are you using and what framework are you targeting?

    Basically, that's correct. Once I've created the API project, the only thing I do is change the authentication process to use the "Work or School Accounts", then I specify On-Premises (for our ADFS server), I specify the .xml file for the on-premises authority and the App ID URI (for development I'm using localhost with a port number). But still, this is just what one might enter going through the project wizard anyway.

    For project type I use Visual C# | Web | ASP.NET Web Application (.NET Framework)

    Tuesday, July 17, 2018 3:53 PM
  • User-900017823 posted


    Hi DoctorWho,

    DoctorWho

    . My guess is that something is changing when new updates to Visual Studio 2017 come out, which breaks compatibility with whatever assemblies is depending upon 'Microsoft.IdentityModel.Protocols.WsFederation.

    Is your previous version running properly?

    if you use the latest Visual Studio 2017, you may can try to update the Microsoft.IdentityModel.Protocols.WsFederation to Version 5.2.4.


    Best Regards,

    Yong Lu

    I did try upgrading the project from Microsoft.IdentityModel.Protocols.WsFederation version 5.2.2 to 5.2.4. And that's what resulted in the error that I'm getting the error that I reported. I'm going to try to uninstall Microsoft.IdentityModel.Protocols.WsFederation, as at this point I'm stuck.

    Tuesday, July 17, 2018 3:56 PM
  • User-900017823 posted

    I got into Package Manager Console, then I issued this command:

    Uninstall-Package Microsoft.IdentityModel.Protocols.WsFederation

    It said that it successfully uninstalled Microsoft.IdentityModel.Protocols.WsFederation. However, when I look at the Web.Config file I still see it there. Why would that be?

    Tuesday, July 17, 2018 4:49 PM
  • User-900017823 posted

    I've re-installed Microsoft.IdentityModel.Protocols.WsFederation. And I still get the same error ("Could not load file or assembly 'Microsoft.IdentityModel.Protocols.WsFederation, Version=5.2.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)")

    Tuesday, July 17, 2018 4:53 PM
  • User475983607 posted

    I've re-installed Microsoft.IdentityModel.Protocols.WsFederation. And I still get the same error ("Could not load file or assembly 'Microsoft.IdentityModel.Protocols.WsFederation, Version=5.2.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)")

    This type of error is common when there is a version mismatch between what the assemblies needs and what's referenced.  It's your job to you to read the docs and make sure you are using the correct DLL versions.  Keep in mind that DLLs are shared so you might have messed up another assembly.

    Have you tried looking at lines 18 and 9 from the error to make sure OWIN is setup properly?

    at MiniCoreWebAPI.Startup.ConfigureAuth(IAppBuilder app) in D:\Src\MiniCoreWebAPI\MiniCoreWebAPI\App_Start\Startup.Auth.cs:line 18
    at MiniCoreWebAPI.Startup.Configuration(IAppBuilder app) in D:\Src\MiniCoreWebAPI\MiniCoreWebAPI\Startup.cs:line 9

    Tuesday, July 17, 2018 5:13 PM
  • User-900017823 posted

    Point taken. However, I followed the suggestion given when I first created the project. When I tried to run it the first time I got an error saying that Microsoft.IdentityModel.Protocols.WsFederation, Version=5.2.0.0 was missing. It was very specific about the version number. So I installed that package. That is what has lead to the errors that I'm mentioned above.

    (I'm sorry, I'd forgotten that I did that step, last week when I first started working on this project. I should have mentioned that right off.)

    Also, about 15 minutes ago I talked with one of my colleagues. He suggested that I install the package Microsoft.Owin.Security.WsFederation. I hadn't done this before. (I was following his instructions from last month when I set up another WebAPI, that worked fine.) However, that didn't help resolve this issue.

    Tuesday, July 17, 2018 9:23 PM
  • User36583972 posted


    Hi DoctorWho,

    May be you can try to Uninstall it and delete the temp wsfederation folder(Exclude the possibility of a several assemblies with the same file name in the probing path.) and reinstall it again.

    C:\Users\yoursername\.nuget\packages\microsoft.identitymodel.protocols.wsfederation

    Managing the global packages, cache, and temp folders
    https://docs.microsoft.com/en-us/nuget/consume-packages/managing-the-global-packages-and-cache-folders

    Best Regards,

    Yong Lu

    Wednesday, July 18, 2018 8:34 AM