【问题标题】:'CspKeyContainerInfo' requires Windows Cryptographic API (CAPI), which is not available on this platform“CspKeyContainerInfo”需要 Windows 加密 API (CAPI),此平台上不可用
【发布时间】:2019-06-08 08:41:20
【问题描述】:

我已将我的项目从 v2.1 升级到 asp.net core v2.2,一切正常工作。在下面显示的代码中,我正在尝试使用 IdentityServer4(v2.3.2) 初始化 RSA 密钥) 并在尝试获取令牌时出现以下错误。

        try
        {
            var rsaProvider = new RSACryptoServiceProvider(2048);

            var rsaParametersPrivate =
                RsaExtensions.RsaParametersFromXmlFile(Configuration.GetSection("JwtSettings:rsaPrivateKeyXml")
                    .Value);
            rsaProvider.ImportParameters(rsaParametersPrivate);
            var securityKey = new RsaSecurityKey(rsaProvider);
            _signingCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.RsaSha256);

            _logger.LogInformation("InitializeRsaKey() successfully executed.");
        }
        catch (Exception ex)
        {
            var exception = new Exception("Identity Server RSA Key initialization failed. " + ex);
            _logger.LogError(exception, "InitializeRsaKey() method failed.");
            throw exception;
        }

“CspKeyContainerInfo”需要 Windows 加密 API (CAPI),此平台上不可用。错误。

另外,我的项目在 CentOS 机器上运行,同时我在 Windows 10 上开发我的项目。所以,我知道 Windows 中存在的东西在 Linux 上丢失了。为了解决这个问题,任何帮助和建议表示赞赏。

【问题讨论】:

    标签: asp.net-core jwt rsa identityserver4 asp.net-core-2.2


    【解决方案1】:

    我挖掘了一些 github 问题,发现 RSACryptoServiceProvider() intherits ICspAsymmetricAlgorithm 并且该类仅在 Windows 上受支持。详情请查看here。为了解决这个问题,我用var rsaProvider = RSA.Create(2048); 替换了var rsaProvider = new RSACryptoServiceProvider(2048); 行,它可以在CentOS 上的.NET Core v2.2 上正常工作。希望这对有同样问题的人有所帮助。

    【讨论】:

    • @konzo 很高兴它帮助了你。
    • 我刚刚在 Linux 上运行的 Docker 容器上尝试了您的建议,效果非常好。
    猜你喜欢
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 2019-05-03
    • 1970-01-01
    • 2011-03-01
    • 2015-04-20
    相关资源
    最近更新 更多