简体   繁体   English

如何将 Azure 托管标识与 Azure.Storage.Blobs.BlobServiceClient 一起使用?

[英]How to use Azure managed identity with Azure.Storage.Blobs.BlobServiceClient?

In v11 SDK for .NET I was able to use managed identity tokens to get access to Azure blob:适用于 .NET 的 v11 SDK 中,我能够使用托管标识令牌来访问 Azure blob:

var token = await new AzureServiceTokenProvider().GetAccessTokenAsync("https://storage.azure.com/");
var tokenCredential = new TokenCredential(token);
var storageCredentials = new StorageCredentials(tokenCredential);
var blob = new CloudBlobContainer(new Uri("https://some_storage.blob.core.windows.net/some_container"), storageCredentials);

Now I want to switch to v12 SDK and can't understand how to do the same for BlobServiceClient .现在我想切换到v12 SDK并且无法理解如何为BlobServiceClient做同样的BlobServiceClient

I have a sample: https://github.com/juunas11/managedidentity-filesharing/blob/8410ed3f3d4061de7d40531c025bf6e474489135/Joonasw.ManagedIdentityFileSharingDemo/Services/AzureBlobStorageService.cs#L80我有一个示例: https : //github.com/juunas11/managedidentity-filesharing/blob/8410ed3f3d4061de7d40531c025bf6e474489135/Joonasw.ManagedIdentityFileSharingDemo/Services/AzureBlobStorageService.cs#L80

Here is how it is used with Azure.Identity:以下是它如何与 Azure.Identity 一起使用:

client = new BlobServiceClient(
    new Uri($"https://{_options.AccountName}.blob.core.windows.net"),
    new ManagedIdentityCredential());

If you need to run against an Azure Storage account locally, you can use a custom TokenCredential, like here: https://github.com/juunas11/Joonasw.ManagedIdentityDemos/blob/3501ee6fff416db7349807e588532da5c3dd24b1/Joonasw.ManagedIdentityDemos/Services/DemoService.cs#L45 .如果需要在本地针对 Azure 存储帐户运行,则可以使用自定义 TokenCredential,例如: https : //github.com/juunas11/Joonasw.ManagedIdentityDemos/blob/3501ee6fff416db7349807e588532da5c3dd24b1/Joonasw.ManagedServices#Services# L45

Custom TokenCredential: https://github.com/juunas11/Joonasw.ManagedIdentityDemos/blob/master/Joonasw.ManagedIdentityDemos/Services/ManagedIdentityStorageTokenCredential.cs .自定义 TokenCredential: https : //github.com/juunas11/Joonasw.ManagedIdentityDemos/blob/master/Joonasw.ManagedIdentityDemos/Services/ManagedIdentityStorageTokenCredential.cs

Usage against Storage Emulator:针对存储模拟器的用法:

client = new BlobServiceClient("UseDevelopmentStorage=true");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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