穆罕默德 - Moh
穆罕默德 - Moh

Reputation: 1373

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

Recently I have to use RSACryptoServiceProvider in dot net core 2.2.

there is something wrong with RSA.FromXmlString() inside my GetSign() method . When I call my Action from UnitTest everything is fine but when I call it from PostMan via Post Method I got below Error on rsa.FromXmlString("XXX") :

operation is not supported on this platform

My Unit Test:

[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 my internal method that called inside controller

        //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);
    }

and this is GetSignt() method that I have problem

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

Upvotes: 0

Views: 2323

Answers (1)

vcsjones
vcsjones

Reputation: 141588

FromXmlString was not implemented fully in .NET Core until .NET Core 3.0. Since .NET Core 2.2 is out of support, you should update to .NET Core 3.1 and it should work.

Upvotes: 1

Related Questions