MSOnline无法加载类型'System.IdentityModel.Tokens.JwtSecurityToken' [英] MSOnline Could not load type 'System.IdentityModel.Tokens.JwtSecurityToken'
问题描述
我有一个使用MSOnline
PowerShell模块与Office 365交互的asp.net核心Web应用程序.当Connect-MsolService
cmdlet执行以与Office 365进行身份验证时,出现以下错误.
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.
无法从程序集"System.IdentityModel.Tokens.Jwt,Version = 5.1.5.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35"中加载类型'System.IdentityModel.Tokens.JwtSecurityToken'.
Could not load type 'System.IdentityModel.Tokens.JwtSecurityToken' from assembly 'System.IdentityModel.Tokens.Jwt, Version=5.1.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
通过单元测试在另一个项目中运行相同的逻辑时,我没有收到错误,代码按预期执行.测试项目和Web项目都使用System.IdentityModel.Tokens.Jwt
程序集的相同版本(5.1.5),所以我不明白为什么在Web应用程序中执行该逻辑时会收到此错误.
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.
我已经读到一个解决方案是降级到System.IdentityModel.Tokens.Jwt
程序集的v4,但我知道它适用于5.1.5,因为我的测试通过了.此外,这对我来说不是一个选择,因为某些aspnetcore程序集需要v5.没有人知道为什么会在asp.net核心Web应用程序中发生这种情况,还是知道不需要降级程序集的解决方案?
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?
更新:
看来我的绑定重定向导致了问题.如果将以下内容添加到测试项目中的app.config文件中,则会产生错误.这很奇怪,因为所引用的System.IdentityModel.Tokens.Jwt
版本是5.1.5,就好像默认使用的是其他版本一样.
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>
推荐答案
我最终通过消除System.IdentityModel.Tokens.Jwt
的绑定重定向解决了这个问题.绑定重定向是自动创建的,因为我正在使用Auto-generate binding redirects
选项.我决定只禁用该选项并手动管理绑定重定向,以消除不必要的重定向.
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.
为简单起见,我只是从输出配置文件中复制了自动生成的绑定重定向,并将其粘贴到我的项目app.config文件中.然后,我删除了System.IdentityModel.Tokens.Jwt
重定向,并禁用了Auto-generate binding redirects
选项,它仅在我的app.config文件中使用了重定向,而不是生成解决问题的重定向.
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.
这篇关于MSOnline无法加载类型'System.IdentityModel.Tokens.JwtSecurityToken'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!