Search code examples
asp.net-corejwtrsaidentityserver4asp.net-core-2.2

'CspKeyContainerInfo' requires Windows Cryptographic API (CAPI), which is not available on this platform


I have upgraded my project to asp.net core v2.2 from v2.1 and everything was used to work just fine.In the code shown below, I am trying to initilaize an RSA Key for with IdentityServer4(v2.3.2) and while trying to get a token I get the following error.

        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' requires Windows Cryptographic API (CAPI), which is not available on this platform. error.

Also, my project runs on a CentOS machine meanwhile I develop my project on Windows 10. So, I am aware that something existing in Windows is missing on Linux. To solve the problem any help and suggestion is appreciated.


Solution

  • I digged some github issues and found out that RSACryptoServiceProvider() intherits ICspAsymmetricAlgorithm and this class is supported only on Windows. For details check out here. To fix the problem I have replaced var rsaProvider = new RSACryptoServiceProvider(2048); line with var rsaProvider = RSA.Create(2048); and it works fine with .NET Core v2.2 on CentOS. Hope this helps those who have the same issue.