Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
powerbidev123
Solution Sage
Solution Sage

Integrate C# code in powerbi report

Requirement:
I need to display a downloadable PDF URL link in a Power BI report. The URL should contain an encrypted ID.

Problem Statement:

  • The downloadable PDF URL is generated using an ID that is encrypted via a third-party DLL from a cryptography package.
  • The encryption and URL generation are implemented in C# code.
  • I need to integrate this encrypted URL into my Power BI report so that users can access and download the PDF.

Query:

  • What is the best approach to include this encrypted PDF URL in Power BI?
  • How can I generate and display this URL dynamically in the report while ensuring proper encryption is applied?
  • Are there any Power BI limitations or best practices I should consider when handling such encrypted URLs?

Would appreciate any guidance on the best way to implement this. Thanks in advance!

2 ACCEPTED SOLUTIONS
Sahir_Maharaj
Super User
Super User

Hello @powerbidev123,

 

Since Power BI cannot run C# code natively, the best approach is to:

 

1. Host the C# encryption logic in an Azure Function, API, or Power Automate connector.
2. Call this API from Power BI to dynamically generate the encrypted URL.

 

a) So basically, a Power Automate Flow will take the unencrypted ID from Power BI.

using System;
using System.Web.Http;
using System.Security.Cryptography;
using System.Text;

public static class EncryptIDFunction
{
    [FunctionName("EncryptID")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req)
    {
        string id = req.Query["id"];
        if (string.IsNullOrEmpty(id)) return new BadRequestObjectResult("Missing ID");

        string encryptedId = EncryptID(id);
        string url = $"https://yourdomain.com/download?file={encryptedId}";
        
        return new OkObjectResult(new { pdfUrl = url });
    }

    private static string EncryptID(string id)
    {
        using (var aes = Aes.Create())
        {
            aes.Key = Encoding.UTF8.GetBytes("your-32-byte-key");
            aes.IV = Encoding.UTF8.GetBytes("your-16-byte-iv");
            
            var encryptor = aes.CreateEncryptor();
            byte[] encryptedBytes = encryptor.TransformFinalBlock(
                Encoding.UTF8.GetBytes(id), 0, id.Length);
            
            return Convert.ToBase64String(encryptedBytes);
        }
    }
}

b) Then, the Flow calls an Azure Function or C# Web API to encrypt the ID.

 

c)Finally, the encrypted URL is returned to Power BI and displayed in a table.


Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution? (Yes, its FREE!)
➤ Lets connect on LinkedIn: Join my network of 15K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ About: https://sahirmaharaj.com/about.html
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning

View solution in original post

Hi @Sahir_Maharaj , let me check this. Will get back to you if this solution works

View solution in original post

2 REPLIES 2
Sahir_Maharaj
Super User
Super User

Hello @powerbidev123,

 

Since Power BI cannot run C# code natively, the best approach is to:

 

1. Host the C# encryption logic in an Azure Function, API, or Power Automate connector.
2. Call this API from Power BI to dynamically generate the encrypted URL.

 

a) So basically, a Power Automate Flow will take the unencrypted ID from Power BI.

using System;
using System.Web.Http;
using System.Security.Cryptography;
using System.Text;

public static class EncryptIDFunction
{
    [FunctionName("EncryptID")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req)
    {
        string id = req.Query["id"];
        if (string.IsNullOrEmpty(id)) return new BadRequestObjectResult("Missing ID");

        string encryptedId = EncryptID(id);
        string url = $"https://yourdomain.com/download?file={encryptedId}";
        
        return new OkObjectResult(new { pdfUrl = url });
    }

    private static string EncryptID(string id)
    {
        using (var aes = Aes.Create())
        {
            aes.Key = Encoding.UTF8.GetBytes("your-32-byte-key");
            aes.IV = Encoding.UTF8.GetBytes("your-16-byte-iv");
            
            var encryptor = aes.CreateEncryptor();
            byte[] encryptedBytes = encryptor.TransformFinalBlock(
                Encoding.UTF8.GetBytes(id), 0, id.Length);
            
            return Convert.ToBase64String(encryptedBytes);
        }
    }
}

b) Then, the Flow calls an Azure Function or C# Web API to encrypt the ID.

 

c)Finally, the encrypted URL is returned to Power BI and displayed in a table.


Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution? (Yes, its FREE!)
➤ Lets connect on LinkedIn: Join my network of 15K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ About: https://sahirmaharaj.com/about.html
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning

Hi @Sahir_Maharaj , let me check this. Will get back to you if this solution works

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.