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?