admin管理员组文章数量:1201787
I am trying to connect with Speech SDK using managed Identity, I dont want to use API key. I followed this article - ;pivots=programming-language-csharp. and created custom domain , assigned required role to myself (for Visual Studio) and the app service. But I am getting this issue - WebSocket upgrade failed: Authentication error (401). Please check subscription information and region name.
Below is my code -
var tokenCredential = new DefaultAzureCredential(new DefaultAzureCredentialOptions{
TenantId = $"{Environment.GetEnvironmentVariable("tenant")}"
});
string token = tokenCredential.GetTokenAsync(
new TokenRequestContext(scopes: new string[] { "/.default" })).GetAwaiter().GetResult().Token;
string authorizationToken = $"aad#{cognitiveResourceId}#{token}";
SpeechConfig Config = SpeechConfig.FromAuthorizationToken(authorizationToken, speechRegion);
Let me know if it is supposed to work with Managed identity or only when you get your token with interactive browser.
I am trying to connect with Speech SDK using managed Identity, I dont want to use API key. I followed this article - https://learn.microsoft.com/en-us/azure/ai-services/speech-service/how-to-configure-azure-ad-auth?tabs=portal&pivots=programming-language-csharp. and created custom domain , assigned required role to myself (for Visual Studio) and the app service. But I am getting this issue - WebSocket upgrade failed: Authentication error (401). Please check subscription information and region name.
Below is my code -
var tokenCredential = new DefaultAzureCredential(new DefaultAzureCredentialOptions{
TenantId = $"{Environment.GetEnvironmentVariable("tenant")}"
});
string token = tokenCredential.GetTokenAsync(
new TokenRequestContext(scopes: new string[] { "https://cognitiveservices.azure.com/.default" })).GetAwaiter().GetResult().Token;
string authorizationToken = $"aad#{cognitiveResourceId}#{token}";
SpeechConfig Config = SpeechConfig.FromAuthorizationToken(authorizationToken, speechRegion);
Let me know if it is supposed to work with Managed identity or only when you get your token with interactive browser.
Share Improve this question asked Jan 21 at 15:45 user25879user25879 1358 bronze badges 2- Can you share your GitHub repo? – Dasari Kamali Commented Jan 22 at 11:34
- Hi, it is just this piece of code, you can create a console application and paste this. And the link above use code, you can try converting a text to speech. Just make sure to give access yourself according as described in the link above on speech service. – user25879 Commented Jan 23 at 6:29
1 Answer
Reset to default 1I created a sample console app using DefaultAzureCredential
and Managed Identity
to work in both development and production respectively.
Code :
using Azure.Identity;
using Azure.Core;
using Microsoft.CognitiveServices.Speech;
class Program
{
static void Main(string[] args)
{
try
{
var tokenCredential = new DefaultAzureCredential(new DefaultAzureCredentialOptions
{
TenantId = $"<tenantID>"
});
string[] scopes = new string[] { "https://cognitiveservices.azure.com/.default" };
var token = tokenCredential.GetTokenAsync(new TokenRequestContext(scopes)).GetAwaiter().GetResult().Token;
string cognitiveResourceId = "<ResourceID>";
string authorizationToken = $"aad#{cognitiveResourceId}#{token}";
string speechRegion = "<speechRegion>";
SpeechConfig speechConfig = SpeechConfig.FromAuthorizationToken(authorizationToken, speechRegion);
SynthesizeTextToSpeech(speechConfig);
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
static void SynthesizeTextToSpeech(SpeechConfig speechConfig)
{
var synthesizer = new SpeechSynthesizer(speechConfig);
string textToSynthesize = "Hello Kamali, how are you?";
var result = synthesizer.SpeakTextAsync(textToSynthesize).GetAwaiter().GetResult();
if (result.Reason == ResultReason.SynthesizingAudioCompleted)
{
Console.WriteLine("Text-to-Speech Synthesis completed successfully.");
}
else
{
Console.WriteLine($"Text-to-Speech synthesis failed: {result.Reason}");
}
}
}
I have assigned the Cognitive Services Speech Contributor role to the service principal and Azure Web App to work locally using DefaultAzureCredential
and in production using Managed Identity
.
I successfully converted the text to speech and heard the audio of the converted speech.
I have successfully deployed the console app to Azure WebJobs.
Logs :
本文标签:
版权声明:本文标题:Managed Identity Authentication for Azure AI Speech - WebSocket upgrade failed: Authentication error (401) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738620731a2103174.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论