web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Power Automate Custom ...
Power Automate
Unanswered

Power Automate Custom Connector C# Code

(0) ShareShare
ReportReport
Posted on 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 

Categories:
I have the same question (1)

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 525 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 324 Moderator

#3
abm abm Profile Picture

abm abm 232 Most Valuable Professional

Last 30 days Overall leaderboard