Unable to upload file to File Share from Virtual Machine using user assigned managed identity.

Gianpaolo Tessitore 10 Reputation points
2025-06-16T13:16:46.6233333+00:00

I'm trying to upload a file to a File Share in a Storage Account using a Managed Idenity from a Azure Virtual Machine.
The identity has the folliwing roles assigned
User's image

and is assigned to the Virtual Machine
User's image

I wrote a small console app to test the connectivity and uploaded it to the VM. This is the code used to initialize the client

            ShareUriBuilder uriBuilder = new(new Uri(resourceUri))
            {
                ShareName = shareName,
                AccountName = accountName
            };
            var credential = new ManagedIdentityCredential(clientId);
            ShareClientOptions clientOptions = new()
            {
                ShareTokenIntent = ShareTokenIntent.Backup
            };
            return new ShareClient(uriBuilder.ToUri(), credential, clientOptions);

when i try to create a folder to store the file using the following code

            await shareClient.GetRootDirectoryClient()
                .GetSubdirectoryClient(path)
                .CreateIfNotExistsAsync(cancellationToken: ct);


i get the following error

Unexpected Exception thrown in : Azure.RequestFailedException: This request is not authorized to perform this operation using this permission.
RequestId:d23dac29-a01a-0035-6abc-defc0e000000
Time:2025-06-16T12:45:41.9240737Z
Status: 403 (This request is not authorized to perform this operation using this permission.)
ErrorCode: AuthorizationPermissionMismatch

Content:


Headers:
Server: Windows-Azure-File/1.0 Microsoft-HTTPAPI/2.0
x-ms-request-id: d23dac29-a01a-0035-6abc-defc0e000000
x-ms-client-request-id: 686235b7-a108-435e-9b4e-2c2c072c429e
x-ms-version: 2025-05-05
x-ms-error-code: AuthorizationPermissionMismatch
Date: Mon, 16 Jun 2025 12:45:41 GMT
Content-Length: 279
Content-Type: application/xml

What am i missing?

Thanks.

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,013 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Alex Burlachenko 9,780 Reputation points
    2025-06-23T07:15:25.5666667+00:00

    hi gianpaolo! thanks for posting this,

    u got the 'storage file data smb share contributor' role assigned to ur managed identity, which is good. but that error screams 'permissions mismatch', so something's off with how ur code talks to azure files.

    u need to tweak ur code a bit. the 'sharetokenintent.backup' is usually for backup scenarios, not regular file ops. try switching it to 'sharetokenintent.file' instead.

    var clientOptions = new ShareClientOptions()
    {
        ShareTokenIntent = ShareTokenIntent.File // not backup!
    };
    
    

    check if ur storage account has 'azure files identity-based auth' enabled. without this, managed identities wont work right. u can find it under 'file shares' in the storage account settings.

    always verify the clientid in ur managedidentitycredential matches the uai1 identity. sometimes copy-paste fails and u auth as the wrong thing. also, storage accounts love network rules - if u blocked public access, make sure the vm's ip or vnet is whitelisted.

    smb shares need port 445 open. azure blocks it by default, so u might need a nat gateway or vnet service endpoint. worth looking into if u still hit walls.

    hope this unsticks u

    rgds,

    Alex


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.