問題描述
在中國區(qū)Azure上,使用Media Service服務(wù),想要使用.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);
默認(rèn)情況下,它們都是指向Global Azure,而非China Azure。
所以,解決當(dāng)前問題的方法就是在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?認(rèn)證,需要設(shè)置以下的環(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
關(guān)于DefaultAzureCredential方法獲取認(rèn)證參數(shù)的順序,如下圖所示:
參考資料
DefaultAzureCredential :?https://learn.microsoft.com/en-us/dotnet/api/overview/azure/identity-readme?view=azure-dotnet#defaultazurecredential文章來源:http://www.zghlxwxcb.cn/news/detail-438232.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-438232.html
到了這里,關(guān)于【Azure 媒體服務(wù)】Media Service的編碼示例 -- 創(chuàng)建縮略圖子畫面的.NET代碼調(diào)試問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!