此平台不支持操作 - dot net core 2 中的 RSACryptoServiceProvider 问题

operation is not supported on this platform - problem with RSACryptoServiceProvider in dot net core 2

最近我不得不在 dot net core 2.2 中使用 RSACryptoServiceProvider。

我的 GetSign() 方法中的 RSA.FromXmlString() 有问题。当我从 UnitTest 调用我的 Action 时,一切都很好,但是当我通过 Post 方法从 PostMan 调用它时,我在 rsa.FromXmlString("XXX" ) :

operation is not supported on this platform

我的单元测试:

[Fact]
        public void TestToken()
        {
            var contoller2 = new BankPaymentController();
            contoller2.GetToken();
       }



//My controller
    [EnableCors("CorsPolicy")]
    [HTTPPOST]
    public IActionResult GetToken()
    {
        BankingInputParam model2 = new BankingInputParam();
           var token = PasargadBank.CallPaymentMethod(model2);
            return Ok(token);
    }

这是我调用内部控制器的内部方法

        //This is CallPaymentMethod
    public static string CallPaymentMethod(BankingInputParam model)
    {
        PasargadBank pasargadbank = new PasargadBank();
        var sendingData = pasargadbank.CreateTextArrayInput(model);
        var content = new StringContent(sendingData, Encoding.UTF8, "application/json");
        var request = new HttpRequestMessage
        {
            RequestUri = new Uri("XXXXXXXXXX"),
            Method = HttpMethod.Post,
            Content = content
        };
        MyClass instancemyclass = new MyClass();
        var aaa = instancemyclass.GetSign(sendingData);

        request.Headers.Add("Sign", pasargadbank.GetSign(sendingData));
        var client = new HttpClient();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        var response = client.SendAsync(request).Result;// requestMessage).Result;
        return Encoding.UTF8.GetString(response.Content.ReadAsByteArrayAsync().Result);
    }

这是我遇到问题的 GetSignt() 方法

public string GetSign(string data) 
{
 var cs = new CspParameters {
  KeyContainerName = "PaymentTest"
 };
 var rsa = new RSACryptoServiceProvider(cs) {
  PersistKeyInCsp = false
 };

 rsa.Clear();
 rsa = new RSACryptoServiceProvider();

 byte[] signMain = rsa.SignData(Encoding.UTF8.GetBytes(data), new SHA1CryptoServiceProvider());
 string sign = Convert.ToBase64String(signMain);
 return sign;
}

FromXmlString 直到 .NET Core 3.0 才在 .NET Core 中完全实现。由于 .NET Core 2.2 不受支持,您应该更新到 .NET Core 3.1,它应该可以工作。