简体   繁体   English

MSOnline 无法加载类型“System.IdentityModel.Tokens.JwtSecurityToken”

[英]MSOnline Could not load type 'System.IdentityModel.Tokens.JwtSecurityToken'

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.我有一个 asp.net 核心 Web 应用程序,它使用MSOnline PowerShell 模块与 Office 365 交互。当Connect-MsolService cmdlet 执行以使用 Office 365 进行身份验证时,我收到以下错误。

Could not load type 'System.IdentityModel.Tokens.JwtSecurityToken' from assembly 'System.IdentityModel.Tokens.Jwt, Version=5.1.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.无法从程序集“System.IdentityModel.Tokens.Jwt,版本=5.1.5.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35”加载类型“System.IdentityModel.Tokens.JwtSecurityToken”。

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.测试项目和 Web 项目都使用System.IdentityModel.Tokens.Jwt程序集的相同版本 (5.1.5),所以我不明白为什么在 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.我读过一个解决方案是降级到System.IdentityModel.Tokens.Jwt程序集的 v4,但我知道它适用于 5.1.5,因为我的测试通过了。 Besides, that's not an option for me because some of the aspnetcore assemblies require v5.此外,这对我来说不是一个选择,因为某些 aspnetcore 程序集需要 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?有谁知道为什么会在 asp.net 核心 Web 应用程序中发生这种情况,或者知道不需要降级程序集的解决方案吗?

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.如果我将以下内容添加到我的测试项目中的 app.config 文件中,则会产生错误。 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.这很奇怪,因为 5.1.5 是所引用的System.IdentityModel.Tokens.Jwt版本,就好像默认情况下使用了不同的版本。

  <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>

I ended up solving this by eliminating the binding redirect for System.IdentityModel.Tokens.Jwt .我最终通过消除System.IdentityModel.Tokens.Jwt的绑定重定向来解决这个问题。 The binding redirect was automatically created because I was using the Auto-generate binding redirects option.绑定重定向是自动创建的,因为我使用了Auto-generate binding redirects选项。 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.为了简单起见,我只是从输出配置文件中复制了自动生成的绑定重定向,并将它们粘贴到我的项目 app.config 文件中。 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.然后,我删除了System.IdentityModel.Tokens.Jwt重定向并禁用了Auto-generate binding redirects选项,它仅使用我的 app.config 文件中的重定向而不是生成它们来解决问题。

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.对我来说幸运的是,情况并非如此,至少目前是这样。

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.我设法通过更新程序集绑定以排除Connect-MsolService正在使用的System.IdentityModel.Tokens.Jwt版本来使其工作。 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure Function | PowerShell | 无法加载类型“System.Security.Cryptography.SHA256Cng” - Azure Function | PowerShell | Could not load type 'System.Security.Cryptography.SHA256Cng' 尝试在ASP.NET Web应用程序中加载Powershell模块“ MSOnline”时出错 - Error while Trying to load Powershell Module “MSOnline” in ASP.NET web app 无法从“C:\Program Files\WindowsPowerShell\Modules\MSOnline\1.1.183.57\PSModule\”加载 PSModule。 试图加载不正确的格式 - Can't load PSModule from 'C:\Program Files\WindowsPowerShell\Modules\MSOnline\1.1.183.57\PSModule\'. An attempt was made to load incorrect format 无法加载文件或程序集“System.Management.Automation,版本 = 3.0.0.0 - Could not load file or assembly 'System.Management.Automation, Version=3.0.0.0 MSOnline同时连接问题 - Problems with simultanious MSOnline connections 无法加载文件或程序集&#39;System.Management.Automation&#39; - Could not load file or assembly 'System.Management.Automation' 复制MSOnline PowerShell模块作为解决方法 - Copying MSOnline PowerShell module as a workaround /global.asax(1):错误ASPPARSE:无法加载类型 - /global.asax(1): error ASPPARSE: Could not load type 添加类型:(0):找不到元数据文件“System.Linq.dll” - Add-Type : (0) : Metadata file 'System.Linq.dll' could not be found 添加类型:无法添加类型。 无法找到程序集“System.IO.Compression.FileSystem” - Add-Type: Cannot add type. The assembly 'System.IO.Compression.FileSystem' could not be found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM