Hi Team,
I have a question about Power Automate Custom Connector Using C# Code. I want to perform RSA Encryption; however, I received an error when I uploaded this code on cloud. The C# script works proper in my localhost; however, the following error occurs in the Custom Connector.
"Failed to provision compute for custom code. Request failed with error 'Code: Bad Request. Message: script.csx(73,28): error CS1061: 'RSACryptoServiceProvider' does not contain a definition for 'ImportSubjectPublicKeyInfo' and no accessible extension method 'ImportSubjectPublicKeyInfo' accepting a first argument of type 'RSACryptoServiceProvider' could be found (are you missing a using directive or an assembly reference?).'. The correlation ID is '3cec6288-8267-45e7-8956-ad050e993949'."
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using Newtonsoft.Json.Linq;
public class Script : ScriptBase
{
public override async Task<HttpResponseMessage> ExecuteAsync()
{
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.BadRequest);
if (this.Context.OperationId == "RSAEncription")
{
return await this.HandleRSAEncryptionOperation().ConfigureAwait(false);
}
response.Content = CreateJsonContent($"Unknown operation ID '{this.Context.OperationId}'");
return response;
}
private async Task<HttpResponseMessage> HandleRSAEncryptionOperation()
{
HttpResponseMessage response;
try
{
var contentAsString = await this.Context.Request.Content.ReadAsStringAsync().ConfigureAwait(false);
var contentAsJson = JObject.Parse(contentAsString);
var PublicKey = (string)contentAsJson["publicKey"];
var OriginalMessage = (string)contentAsJson["originalMessage"];
// Convert the Base64 strings to byte arrays
byte[] publicKeyBytes = Convert.FromBase64String(PublicKey);
// Import public key parameters using RSA.Create()
using (RSACryptoServiceProvider rsaEncrypt = new RSACryptoServiceProvider())
{
rsaEncrypt.ImportSubjectPublicKeyInfo(publicKeyBytes, out _);
// Your data to be encrypted
string dataToEncrypt = "Hello, this is a test message.";
// Convert the string to byte array
byte[] dataBytes = Encoding.UTF8.GetBytes(dataToEncrypt);
// Encrypt the data
byte[] encryptedData = rsaEncrypt.Encrypt(dataBytes, false);
JObject output = new JObject
{
["OriginalMessage"] = OriginalMessage,
["EncryptedMessage"] = Convert.ToBase64String(encryptedData)
};
response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = CreateJsonContent(output.ToString());
}
}
catch (Exception ex)
{
response = new HttpResponseMessage(HttpStatusCode.InternalServerError);
response.Content = CreateJsonContent($"An error occurred: {ex.Message}");
}
return response;
}
}
Thanks