简体   繁体   中英

Azure - Use MSAL to generate AccessToken which can be used to access BlobServiceClient

At present, I am using the AcquireTokenInteractive.ExecuteAsync() function to gather an AccessToken which I can then use to ensure that user's accessing Azure are only permitted access to resources they have sufficient permissions for.

However, when I look at the ways that I can instantiate a BlobServiceClient object, the only way in which I can construct the object using a Token is by using the following constructors: (Uri, TokenCredential, BlobClientOptions)

Is there any way I can use the AuthenticationResult object that is returned by the AcquireTokenInteractive.ExecuteAsync() call to create an object that can be passed in when creating the BlobServiceClient that would satisfy the TokenCredential constructor?

tl;dr: I want to access Azure Storage using an AccessToken that has been created by AcquireTokenInteractive

Regarding the issue, I suggest you use InteractiveBrowserCredential credential. It will launch the system default browser to interactively authenticate a user, and obtain an access token. But, please note that the user used to do auth should have the role Storage Blob Data Contributor etc. For more details, please refer to here and here .

For example

            var blobServiceClient =new BlobServiceClient(new Uri($"https://{storageAccountName}.blob.core.windows.net"), new InteractiveBrowserCredential());
            await foreach (var container in blobServiceClient.GetBlobContainersAsync()) {
                Console.WriteLine(container.Name);
            }

在此处输入图像描述


Update

var cred = new InteractiveBrowserCredential();
            BlobClient client1 = new BlobClient(new Uri("https://<>.blob.core.windows.net/test/faces.jpg"), cred);

            BlobProperties pro = await client1.GetPropertiesAsync();
            Console.WriteLine(pro.ContentLength);

            BlobClient client2 = new BlobClient(new Uri("https://<>.blob.core.windows.net/test/test.csv"), cred);
            pro =await client2.GetPropertiesAsync();

            Console.WriteLine(pro.ContentLength);

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM