Blazor WebAssembly 不支持 System.Security.Cryptography API

在浏览器上运行时,System.Security.Cryptography API 在运行时引发 PlatformNotSupportedException

更改描述

在以前的 .NET 版本中,大多数 System.Security.Cryptography API 不适用于 Blazor WebAssembly 应用。 从 .NET 5 开始,Blazor WebAssembly 应用面向完整的 .NET 5 API 外围应用,但由于浏览器沙盒约束,并非所有 .NET 5 API 都受支持。 在 .NET 5 及更高版本中,不支持 System.Security.Cryptography 的 API 会在 WebAssembly 上运行时引发 PlatformNotSupportedException

小窍门

生成支持浏览器平台的项目时 ,平台兼容性分析器 将标记对受影响 API 的任何调用。 此分析器默认在 .NET 5 及更高版本中运行。

更改原因

Microsoft无法在 Blazor WebAssembly 配置中将 OpenSSL 作为依赖项交付。 我们尝试通过尝试与浏览器的 SubtleCrypto API 集成来解决此问题。 遗憾的是,它需要进行重大 API 更改,使它难以集成。

已引入的版本

5.0

目前没有很好的解决方法建议。

受影响的 API

除以下 API 之外的所有 System.Security.Cryptography API:

  • System.Security.Cryptography.RandomNumberGenerator
  • System.Security.Cryptography.IncrementalHash
  • System.Security.Cryptography.SHA1
  • System.Security.Cryptography.SHA256
  • System.Security.Cryptography.SHA384
  • System.Security.Cryptography.SHA512
  • System.Security.Cryptography.SHA1Managed
  • System.Security.Cryptography.SHA256Managed
  • System.Security.Cryptography.SHA384Managed
  • System.Security.Cryptography.SHA512Managed