Skip to main content

Notifications

Community site session details

Community site session details

Session Id : YkAwZYIGvgS3Vqv1n/p0GV
Power Automate - Connector Development
Unanswered

Power Automate Custom Connector C# Code

Like (0) ShareShare
ReportReport
Posted on 8 Jan 2024 12:11:31 by

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 

@ManishSolanki @Pstork1 

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Automate - Connector Development

#1
sdr7893 Profile Picture

sdr7893 2

Overall leaderboard

Featured topics

Loading started