Jens Chr. I. Thomsen
Jens Chr. I. Thomsen

Reputation: 41

Not authorized to create folder on Azure File Share

I have a blazor server app that is hosted on Azure and I am using AAD to log my user on to the system.

I also let the user create folders and upload files to Azure File Share. And here is my problem. When I run the solution from Visual Studio I have no problem creating a folder and upload a file. But if I run the webapp after I have published it from VS then i get an error.

Status: 403 (This request is not authorized to perform this operation.)

This is the code I am using

// Initialize the ShareServiceClient with your connection string
ShareServiceClient serviceClient = new ShareServiceClient(_connectionString);

// Get a reference to the File Share
ShareClient shareClient = serviceClient.GetShareClient("projectfiles");

// Get a reference to the directory where you want to create the folder
ShareDirectoryClient directoryClient = shareClient.GetDirectoryClient(folderCreateString);

// Create the folder
directoryClient.CreateIfNotExists();

My method has this tag [Authorize(Policy = "Eventum.Users")]

I am not an expert in Azure but guess it is a permission problem, I just don't know how to fix it.

Can anyone guide me in the right direction?

Upvotes: 1

Views: 453

Answers (1)

Dasari Kamali
Dasari Kamali

Reputation: 3553

I tried the below Blazor server code with AAD and was able to create a file share, folder and file in the Azure Storage File Share.

Code :

AzureStorageService.cs :

using Azure;
using Azure.Storage.Files.Shares;
using Azure.Storage.Files.Shares.Models;
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace XXXXXblazor
{
    public class AzureStorageService
    {
        private readonly string connectionString;

        public AzureStorageService(string connectionString)
        {
            this.connectionString = connectionString;
        }

        public async Task CreateShareAsync(string shareName)
        {
            var shareServiceClient = new ShareServiceClient(connectionString);
            var shareClient = shareServiceClient.GetShareClient(shareName);

            await shareClient.CreateAsync();
        }

        public async Task CreateDirectoryAsync(string shareName, string directoryName)
        {
            var shareServiceClient = new ShareServiceClient(connectionString);
            var shareClient = shareServiceClient.GetShareClient(shareName);
            var directoryClient = shareClient.GetDirectoryClient(directoryName);

            await directoryClient.CreateAsync();
        }

        public async Task CreateFileAsync(string shareName, string directoryName, string fileName, Stream stream)
        {
            var shareServiceClient = new ShareServiceClient(connectionString);
            var shareClient = shareServiceClient.GetShareClient(shareName);
            var directoryClient = shareClient.GetDirectoryClient(directoryName);
            var fileClient = directoryClient.GetFileClient(fileName);

            await fileClient.CreateAsync(stream.Length);
            await fileClient.UploadRangeAsync(new HttpRange(0, stream.Length), stream);
        }
    }
}

index.razor :

@page "/"
<h3>Azure Storage Demo</h3>
<button @onclick="CreateShare">Create Share</button>
<button @onclick="CreateDirectory">Create Directory</button>
<button @onclick="UploadFile">Upload File</button>

@code {
    private string connectionString = "<Storage_Connec_string>";
    private AzureStorageService storageService;

    private string shareName = "<FileShare_name>";
    private string directoryName = "<folder_name>";
    private string fileName = "<file_name>.txt";

    private async Task CreateShare()
    {
        storageService = new AzureStorageService(connectionString);
        await storageService.CreateShareAsync(shareName);
    }

    private async Task CreateDirectory()
    {
        if (storageService == null)
        {
            storageService = new AzureStorageService(connectionString);
        }

        await storageService.CreateDirectoryAsync(shareName, directoryName);
    }

    private async Task UploadFile()
    {
        if (storageService == null)
        {
            storageService = new AzureStorageService(connectionString);
        }
        byte[] fileContent = GetFileContent("Hello, Azure Storage!");

        using (var stream = new MemoryStream(fileContent))
        {
            await storageService.CreateFileAsync(shareName, directoryName, fileName, stream);
        }
    }

    private byte[] GetFileContent(string content)
    {
        return System.Text.Encoding.UTF8.GetBytes(content);
    }
}

Output :

It runs successfully as below:

enter image description here

I got the below output in the browser. Then, I click on Create Share, Create Directory, and Upload File to create a share, directory and file in the Azure storage.

enter image description here

Azure Portal :

The file share, directory and file created in the Azure storage as below:

enter image description here

I published the above code to the Azure web app service as below:

enter image description here

I added the below Redirect URI in the Azure Active Directory App below:

enter image description here

Then, I browse the web app like below:

enter image description here

I click on the Create Share, Create Directory and Upload File to create a share, directory and file in the Azure storage as below:

enter image description here

Azure Portal :

The file share, directory and file created in the Azure storage as below.

enter image description here

Upvotes: 0

Related Questions