azurex++dynamics-365-operations

Access azure blob from x++ to upload file through managed identity


we have utlise this link and others to upload a file from x++ to azure blob via SAS token. However, we want to achive the same via managed identity as the internal security has forbidden usage of Keys. I know that this involvs

  1. Registering an Appid
  2. Providing IAM access to the blob storage as data contributor/data owner role for the app id.
  3. Generating access token in x++ based on tenantid, appid, secret, scope
  4. utilise the access token to upload the file to azure blob.

I am unable to achieve step 4 via x++ code. Please help and also suggest any alternative OOB solution if applicable.

thanks.


Solution

  • Initially, I registered one application and granted Storage API permission in it as below:

    enter image description here

    Under storage account, I added "Storage Blob Data Contributor" role to above application like this:

    enter image description here

    In my case, I used below sample c# code to upload file to Azure Storage account:

    using Azure.Identity;
    using Azure.Storage.Blobs;
    using Azure.Storage.Blobs.Models;
    
    namespace AzureBlobUploadApp
    {
        class Program
        {
            private static async Task Main(string[] args)
            {
                string tenantId = "tenantId";
                string clientId = "appId";
                string clientSecret = "secret";
                string storageAccountName = "sridemostor1411";
                string containerName = "sri";
                string blobName = "logo.jpg";
                string filePath = "C:\\test\\logo.jpg";
    
                var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
                await UploadFileToBlobAsync(storageAccountName, containerName, blobName, filePath, credential);
            }
    
            private static async Task UploadFileToBlobAsync(string storageAccountName, string containerName, string blobName, string filePath, ClientSecretCredential credential)
            {
                string blobUri = $"https://{storageAccountName}.blob.core.windows.net/{containerName}/{blobName}";
                var blobClient = new BlobClient(new Uri(blobUri), credential);
    
                using FileStream fileStream = File.OpenRead(filePath);
                await blobClient.UploadAsync(fileStream, new BlobHttpHeaders { ContentType = "application/octet-stream" });
    
                Console.WriteLine("File uploaded successfully!");
            }
        }
    }
    

    Response:

    enter image description here

    To confirm that, I checked the same in Azure Portal where file uploaded successfully as below:

    enter image description here