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

Announcements

News and Announcements icon
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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the April Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
Vish WR Profile Picture

Vish WR 785

#2
Valantis Profile Picture

Valantis 669

#3
Haque Profile Picture

Haque 563

Last 30 days Overall leaderboard