問題描述
在中國區(qū)Azure上,使用Media Service服務盯仪,想要使用.NET的代碼來對上傳視頻創(chuàng)建縮略圖(Thumbnail) 。
通過官網(wǎng)文檔(https://docs.azure.cn/zh-cn/media-services/latest/samples/samples-encoding-reference#create-a-thumbnail-sprite)下載.NET示例,配置 appsettings.json 中的參數(shù)匿醒,運行卻出現(xiàn)(Azure.Identity.AuthenticationFailedException: 'ClientSecretCredential authentication failed: AADSTS90002: )異常睡榆。
Azure.Identity.AuthenticationFailedException: 'ClientSecretCredential authentication failed: AADSTS90002: Tenant '********-****-****-****-************' not found. Check to make sure you have the correct tenant ID and are signing into the correct cloud. Check with your subscription administrator, this may happen if there are no active subscriptions for the tenant.
Trace ID: 99b963f7-86a5-4cde-a890-8828eff73000
Correlation ID: 62d4fa3b-92ad-4411-850c-87f562a256b3
Timestamp: 2023-05-10 07:25:55Z'
問題解答
查看.NET項目中的源碼萍肆,發(fā)現(xiàn)獲取Credential的代碼使用的是 DefaultAzureCredential()袍榆。并且 ArmClient 對象也沒有指定Azure的運行環(huán)境。
var mediaServicesResourceId = MediaServicesAccountResource.CreateResourceIdentifier(
subscriptionId: options.AZURE_SUBSCRIPTION_ID.ToString(),
resourceGroupName: options.AZURE_RESOURCE_GROUP,
accountName: options.AZURE_MEDIA_SERVICES_ACCOUNT_NAME);
var credential = new DefaultAzureCredential(includeInteractiveCredentials: true);
var armClient = new ArmClient(credential);
var mediaServicesAccount = armClient.GetMediaServicesAccountResource(mediaServicesResourceId);
默認情況下塘揣,它們都是指向Global Azure包雀,而非China Azure。
所以亲铡,解決當前問題的方法就是在DefaultAzureCredential和ArmClient方法中指定中國區(qū)Azure為運行環(huán)境才写。
修改這部分代碼為為:
var mediaServicesResourceId = MediaServicesAccountResource.CreateResourceIdentifier(
subscriptionId: options.AZURE_SUBSCRIPTION_ID.ToString(),
resourceGroupName: options.AZURE_RESOURCE_GROUP,
accountName: options.AZURE_MEDIA_SERVICES_ACCOUNT_NAME);
DefaultAzureCredentialOptions dacOptions = new DefaultAzureCredentialOptions() { AuthorityHost = AzureAuthorityHosts.AzureChina };
var credential = new DefaultAzureCredential(dacOptions);
ArmClientOptions armOptions = new ArmClientOptions() { Environment = ArmEnvironment.AzureChina};
var armClient = new ArmClient(credential, options.AZURE_SUBSCRIPTION_ID.ToString(), armOptions);
var mediaServicesAccount = armClient.GetMediaServicesAccountResource(mediaServicesResourceId);
注意:使用 **DefaultAzureCredential **認證,需要設置以下的環(huán)境變量
- AZURE_CLIENT_ID
- AZURE_TENANT_ID
- AZURE_CLIENT_SECRET
變量說明: https://learn.microsoft.com/en-us/dotnet/api/overview/azure/identity-readme?view=azure-dotnet#environment-variables
關于DefaultAzureCredential方法獲取認證參數(shù)的順序奖蔓,如下圖所示:
參考資料
DefaultAzureCredential : https://learn.microsoft.com/en-us/dotnet/api/overview/azure/identity-readme?view=azure-dotnet#defaultazurecredential
當在復雜的環(huán)境中面臨問題赞草,格物之道需:濁而靜之徐清,安以動之徐生吆鹤。 云中厨疙,恰是如此!