MSOnline Could not load type 'System.IdentityModel.Tokens.JwtSecurityToken'

≯℡__Kan透↙ 提交于 2020-06-17 09:12:03

问题


I have an asp.net core web application that uses the MSOnline PowerShell module to interact with Office 365. When the Connect-MsolService cmdlet executes to authenticate with Office 365 I'm getting the following error.

Could not load type 'System.IdentityModel.Tokens.JwtSecurityToken' from assembly 'System.IdentityModel.Tokens.Jwt, Version=5.1.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

When running the same logic in another project via a unit test I do not get the error, the code executes as expected. Both the test project and the web project are using the same version (5.1.5) of the System.IdentityModel.Tokens.Jwt assembly so I don't understand why I'm getting this error when that logic executes in the web app.

I've read that a solution is to downgrade to v4 of the System.IdentityModel.Tokens.Jwt assembly but I know it works with 5.1.5 because my tests are passing. Besides, that's not an option for me because some of the aspnetcore assemblies require v5. Does anyone understand why this would happen in the asp.net core web app or know a solution that doesn't require downgrading the assembly?

Update:

It looks like my binding redirect is causing the problem. If I add the following to the app.config file in my test project it produces the error. This is very odd because 5.1.5 is the version of System.IdentityModel.Tokens.Jwt that's referenced, it's as if a different version is being used by default.

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.IdentityModel.Tokens.Jwt" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.4.0.0" newVersion="5.1.5.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

回答1:


I ended up solving this by eliminating the binding redirect for System.IdentityModel.Tokens.Jwt. The binding redirect was automatically created because I was using the Auto-generate binding redirects option. I decided to just disable that option and manage the binding redirects manually to eliminate the unwanted redirect.

To make it simple I just copied the auto generated binding redirects from the output config file and pasted them into my projects app.config file. Then, I removed the System.IdentityModel.Tokens.Jwt redirect and with the Auto-generate binding redirects option disabled it only used the redirects in my app.config file instead of generating them which solved the problem.

I still don't understand why the redirect causes that error, hopefully someone will eventually shed some light on that but luckily I found a workaround.

One thing to note, if any of your dependencies use different versions of that assembly this obviously won't work. Luckily for me that isn't the case, at least for now.




回答2:


I managed to get this to work by updating the assembly binding to exclude the version of System.IdentityModel.Tokens.Jwt that the Connect-MsolService was using. This worked. My updated assembly binding is:

<dependentAssembly>
        <assemblyIdentity name="System.IdentityModel.Tokens.Jwt" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="5.0.0.0-5.6.0.0" newVersion="5.6.0.0" />
</dependentAssembly>


来源:https://stackoverflow.com/questions/54837221/msonline-could-not-load-type-system-identitymodel-tokens-jwtsecuritytoken

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!