Dela via


Microsoft Entra-autentisering med Speech SDK

När du använder Speech SDK för att komma åt Speech-tjänsten finns det tre tillgängliga autentiseringsmetoder: tjänstnycklar, en nyckelbaserad token och Microsoft Entra-ID. Den här artikeln beskriver hur du konfigurerar en Speech-resurs och skapar ett Speech SDK-konfigurationsobjekt för att använda Microsoft Entra-ID för autentisering.

Den här artikeln visar hur du använder Microsoft Entra-autentisering med Speech SDK. Du lär dig att:

  • Skapa en Speech-resurs
  • Konfigurera Speech-resursen för Microsoft Entra-autentisering
  • Hämta en Microsoft Entra-åtkomsttoken
  • Skapa rätt SDK-konfigurationsobjekt.

Mer information om Microsoft Entra-åtkomsttoken, inklusive tokenlivslängd, finns i Åtkomsttoken i Microsofts identitetsplattform.

Skapa en Speech-resurs

Information om hur du skapar en Speech-resurs i Azure Portal finns i den här snabbstarten.

Konfigurera Speech-resursen för Microsoft Entra-autentisering

Om du vill konfigurera din Speech-resurs för Microsoft Entra-autentisering skapar du ett anpassat domännamn och tilldelar roller.

Skapa ett anpassat domännamn

Följ de här stegen för att skapa ett anpassat underdomännamn för Azure AI-tjänster för din Speech-resurs.

Varning

När du aktiverar ett anpassat domännamn är åtgärden inte reversibel. Det enda sättet att gå tillbaka till det regionala namnet är att skapa en ny Speech-resurs.

Om din Speech-resurs har många associerade anpassade modeller och projekt som skapats via Speech Studio rekommenderar vi starkt att du provar konfigurationen med en testresurs innan du ändrar resursen som används i produktion.

Följ dessa steg för att skapa ett anpassat domännamn med hjälp av Azure Portal:

  1. Gå till Azure-portalen och logga in på ditt Azure-konto.

  2. Välj den talresurs som krävs.

  3. I gruppen Resurshantering i den vänstra rutan väljer du Nätverk.

  4. På fliken Brandväggar och virtuella nätverk väljer du Generera anpassat domännamn. En ny högerpanel visas med instruktioner för att skapa en unik anpassad underdomän för resursen.

  5. I panelen Generera anpassat domännamn anger du ett anpassat domännamn. Din fullständiga anpassade domän ser ut så här: https://{your custom name}.cognitiveservices.azure.com.

    Kom ihåg att när du har skapat ett anpassat domännamn kan det inte ändras.

    När du har angett ditt anpassade domännamn väljer du Spara.

  6. När åtgärden är klar väljer du Nycklar och slutpunkt i resurshanteringsgruppen. Bekräfta att resursens nya slutpunktsnamn börjar så här: https://{your custom name}.cognitiveservices.azure.com.

Tilldela roller

För Microsoft Entra-autentisering med Speech-resurser måste du tilldela rollen Cognitive Services Speech-deltagare eller Cognitive Services Speech-användare .

Du kan tilldela roller till användaren eller programmet med hjälp av Azure Portal eller PowerShell.

Hämta en Microsoft Entra-åtkomsttoken

Om du vill hämta en Microsoft Entra-åtkomsttoken i C# använder du Azure Identity Client Library.

Här är ett exempel på hur du använder Azure Identity för att hämta en Microsoft Entra-åtkomsttoken från en interaktiv webbläsare:

TokenRequestContext context = new Azure.Core.TokenRequestContext(new string[] { "https://cognitiveservices.azure.com/.default" });
InteractiveBrowserCredential browserCredential = new InteractiveBrowserCredential();
var browserToken = browserCredential.GetToken(context);
string aadToken = browserToken.Token;

Kommentar

Tokenkontexten måste anges till "https://cognitiveservices.azure.com/.default".

Om du vill hämta en Microsoft Entra-åtkomsttoken i C++använder du Azure Identity Client Library.

Här är ett exempel på hur du använder Azure Identity för att hämta en Microsoft Entra-åtkomsttoken med ditt klient-ID, klient-ID och autentiseringsuppgifter för klienthemligheter:

const std::string tokenContext = "https://cognitiveservices.azure.com/.default";

Azure::Identity::DefaultAzureCredential();

Azure::Core::Credentials::TokenRequestContext context;
context.Scopes.push_back(tokenContext);

auto token = cred.GetToken(context, Azure::Core::Context());

Kommentar

Tokenkontexten måste anges till "https://cognitiveservices.azure.com/.default".

Om du vill hämta en Microsoft Entra-åtkomsttoken i Java använder du Azure Identity Client Library.

Här är ett exempel på hur du använder Azure Identity för att hämta en Microsoft Entra-åtkomsttoken från en webbläsare:

TokenRequestContext context = new TokenRequestContext();
context.addScopes("https://cognitiveservices.azure.com/.default");

InteractiveBrowserCredentialBuilder builder = new InteractiveBrowserCredentialBuilder();
InteractiveBrowserCredential browserCredential = builder.build();

AccessToken browserToken = browserCredential.getToken(context).block();
String token = browserToken.getToken();

Kommentar

Tokenkontexten måste anges till "https://cognitiveservices.azure.com/.default".

Om du vill hämta en Microsoft Entra-åtkomsttoken i Python använder du Azure Identity Client Library.

Här är ett exempel på hur du använder Azure Identity för att hämta en Microsoft Entra-åtkomsttoken från en interaktiv webbläsare:

from azure.identity import  InteractiveBrowserCredential
ibc = InteractiveBrowserCredential()
aadToken = ibc.get_token("https://cognitiveservices.azure.com/.default")

Hitta exempel som hämtar en Microsoft Entra-åtkomsttoken i Microsofts identitetsplattform kodexempel.

För programmeringsspråk där ett Microsofts identitetsplattform klientbibliotek inte är tillgängligt kan du begära en åtkomsttoken direkt.

Hämta resurs-ID för Tal

Du behöver ditt Speech-resurs-ID för att göra SDK-anrop med Microsoft Entra-autentisering.

Kommentar

Använd ditt LUIS Prediction-resurs-ID för avsiktsigenkänning.

Så här hämtar du resurs-ID:t i Azure Portal:

  1. Gå till Azure-portalen och logga in på ditt Azure-konto.
  2. Välj en Speech-resurs.
  3. I gruppen Resurshantering i den vänstra rutan väljer du Egenskaper.
  4. Kopiera resurs-ID: t

Skapa speech SDK-konfigurationsobjektet

Med en Microsoft Entra-åtkomsttoken kan du nu skapa ett Speech SDK-konfigurationsobjekt.

Metoden för att tillhandahålla token och metoden för att konstruera motsvarande Speech SDK-objekt Config varierar beroende på vilket objekt du använder.

SpeechRecognizer, SpeechSynthesizer, IntentRecognizer, ConversationTranscriber

För SpeechRecognizer, SpeechSynthesizer, IntentRecognizer, ConversationTranscriber objekt skapar du auktoriseringstoken från resurs-ID:t och Microsoft Entra-åtkomsttoken och använder den sedan för att skapa ett SpeechConfig objekt.

string resourceId = "Your Resource ID";
string aadToken = "Your Microsoft Entra access token";
string region =  "Your Speech Region";

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and Microsoft Entra access token.
var authorizationToken = $"aad#{resourceId}#{aadToken}";
var speechConfig = SpeechConfig.FromAuthorizationToken(authorizationToken, region);
std::string resourceId = "Your Resource ID";
std::string aadToken = "Your Microsoft Entra access token";
std::string region = "Your Speech Region";

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and Microsoft Entra access token.
auto authorizationToken = "aad#" + resourceId + "#" + aadToken;
auto speechConfig = SpeechConfig::FromAuthorizationToken(authorizationToken, region);
String resourceId = "Your Resource ID";
String region = "Your Region";

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and Microsoft Entra access token.
String authorizationToken = "aad#" + resourceId + "#" + token;
SpeechConfig speechConfig = SpeechConfig.fromAuthorizationToken(authorizationToken, region);
resourceId = "Your Resource ID"
region = "Your Region"
# You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and Microsoft Entra access token.
authorizationToken = "aad#" + resourceId + "#" + aadToken.token
speechConfig = SpeechConfig(auth_token=authorizationToken, region=region)

TranslationRecognizer

TranslationRecognizerFör skapar du auktoriseringstoken från resurs-ID:t och Microsoft Entra-åtkomsttoken och använder den sedan för att skapa ett SpeechTranslationConfig objekt.

string resourceId = "Your Resource ID";
string aadToken = "Your Microsoft Entra access token";
string region =  "Your Speech Region";

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and Microsoft Entra access token.
var authorizationToken = $"aad#{resourceId}#{aadToken}";
var speechConfig = SpeechTranslationConfig.FromAuthorizationToken(authorizationToken, region);
std::string resourceId = "Your Resource ID";
std::string aadToken = "Your Microsoft Entra access token";
std::string region = "Your Speech Region";

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and Microsoft Entra access token.
auto authorizationToken = "aad#" + resourceId + "#" + aadToken;
auto speechConfig = SpeechTranslationConfig::FromAuthorizationToken(authorizationToken, region);
String resourceId = "Your Resource ID";
String region = "Your Region";

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and Microsoft Entra access token.
String authorizationToken = "aad#" + resourceId + "#" + token;
SpeechTranslationConfig translationConfig = SpeechTranslationConfig.fromAuthorizationToken(authorizationToken, region);
resourceId = "Your Resource ID"
region = "Your Region"

# You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and Microsoft Entra access token.
authorizationToken = "aad#" + resourceId + "#" + aadToken.token
translationConfig = SpeechTranslationConfig(auth_token=authorizationToken, region=region)

VoiceProfileClient

Om du vill använda VoiceProfileClient med Microsoft Entra-autentisering använder du det anpassade domännamnet som skapades ovan.

string customDomainName = "Your Custom Name";
string hostName = $"https://{customDomainName}.cognitiveservices.azure.com/";
string token = "Your Microsoft Entra access token";

var config =  SpeechConfig.FromHost(new Uri(hostName));

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and Microsoft Entra access token.
var authorizationToken = $"aad#{resourceId}#{aadToken}";
config.AuthorizationToken = authorizationToken;
std::string customDomainName = "Your Custom Name";
std::string aadToken = "Your Microsoft Entra access token";

auto speechConfig = SpeechConfig::FromHost("https://" + customDomainName + ".cognitiveservices.azure.com/");

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and Microsoft Entra access token.
auto authorizationToken = "aad#" + resourceId + "#" + aadToken;
speechConfig->SetAuthorizationToken(authorizationToken);
String aadToken = "Your Microsoft Entra access token";
String customDomainName = "Your Custom Name";
String hostName = "https://" + customDomainName + ".cognitiveservices.azure.com/";
SpeechConfig speechConfig = SpeechConfig.fromHost(new URI(hostName));

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and Microsoft Entra access token.
String authorizationToken = "aad#" + resourceId + "#" + token;

speechConfig.setAuthorizationToken(authorizationToken);

VoiceProfileClient Är inte tillgängligt med Speech SDK för Python.

Kommentar

ConversationTranslator Stöder inte Microsoft Entra-autentisering.