Autenticazione di Microsoft Entra con Speech SDK

Quando si usa Speech SDK per accedere al servizio Voce, sono disponibili tre metodi di autenticazione: chiavi del servizio, token basato su chiave e Microsoft Entra ID. Questo articolo spiega come configurare una risorsa Voce e creare un oggetto di configurazione di Speech SDK per poter usare Microsoft Entra ID per l'autenticazione.

Questo articolo mostra come usare l'autenticazione di Microsoft Entra con Speech SDK. Scopri come:

  • Creare una risorsa Voce
  • Configurare la risorsa Voce per l'autenticazione di Microsoft Entra
  • Ottenere un token di accesso di Microsoft Entra
  • Creare l'oggetto di configurazione dell'SDK appropriato.

Per altre informazioni sui token di accesso di Microsoft Entra, inclusa la durata dei token, vedere Token di accesso in Microsoft Identity Platform.

Creare una risorsa Voce

Per creare una risorsa Voce nel portale di Azure, vedere questa guida rapida.

Configurare la risorsa Voce per l'autenticazione di Microsoft Entra

Per configurare la risorsa Voce per l'autenticazione di Microsoft Entra, creare un nome di dominio personalizzato e assegnare i ruoli.

Creare un nome di dominio personalizzato

Seguire questa procedura per creare un nome di sottodominio personalizzato per i servizi di intelligenza artificiale di Azure per la risorsa Voce.

Attenzione

Quando si attiva un nome di dominio personalizzato, l'operazione non è reversibile. L'unico modo per tornare al nome locale consiste nel creare una nuova risorsa Voce.

Se la risorsa Voce ha molti modelli e progetti personalizzati associati creati tramite Speech Studio, è consigliabile provare la configurazione con una risorsa di test prima di modificare la risorsa usata nell'ambiente di produzione.

Per creare un nome di dominio personalizzato usando il portale di Azure, seguire questa procedura:

  1. Passare al portale di Azure e accedere all'account Azure.

  2. Selezionare la risorsa Voce necessaria.

  3. Nel gruppo Gestione risorse nel riquadro sinistro, selezionare Rete.

  4. Nella scheda Firewall e reti virtuali selezionare Genera nome di dominio personalizzato. Verrà visualizzato un nuovo pannello a destra con le istruzioni per creare un sottodominio personalizzato univoco per la risorsa.

  5. Nel pannello Genera nome di dominio personalizzato immettere un nome di dominio personalizzato. Il dominio personalizzato completo sarà simile al seguente: https://{your custom name}.cognitiveservices.azure.com.

    Tenere presente che, dopo aver creato un nome di dominio personalizzato, questo non può essere modificato.

    Dopo aver immesso il nome di dominio personalizzato, selezionare Salva.

  6. Al termine dell'operazione, nel gruppo Gestione risorse selezionare Chiavi ed endpoint. Verificare che il nuovo nome endpoint della risorsa inizi in questo modo: https://{your custom name}.cognitiveservices.azure.com.

Assegnazione di ruoli

Per l'autenticazione di Microsoft Entra con le risorse Voce, è necessario assegnare il ruolo di collaboratore Voce dei Servizi cognitivi o utente Voce di Servizi cognitivi.

È possibile assegnare ruoli all'utente o all'applicazione usando il portale di Azure o PowerShell.

Ottenere un token di accesso di Microsoft Entra

Per ottenere un token di accesso di Microsoft Entra in C#, usare la libreria client delle identità di Azure.

Ecco un esempio dell'uso dell’identità di Azure per ottenere un token di accesso di Microsoft Entra da un browser interattivo:

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;

Il contesto del token deve essere impostato su "https://cognitiveservices.azure.com/.default".

Per ottenere un token di accesso di Microsoft Entra in C++, usare la libreria client delle identità di Azure.

Ecco un esempio dell'uso dell’identità di Azure per ottenere un token di accesso di Microsoft Entra con l'ID tenant, l'ID client e le credenziali del segreto client:

const std::string tenantId = "Your Tenant ID";
const std::string clientId = "Your Client ID";
const std::string clientSecret = "Your Client Secret";
const std::string tokenContext = "https://cognitiveservices.azure.com/.default";

Azure::Identity::ClientSecretCredential cred(tenantId,
    clientId,
    clientSecret,
    Azure::Identity::ClientSecretCredentialOptions());

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

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

Il contesto del token deve essere impostato su "https://cognitiveservices.azure.com/.default".

Per ottenere un token di accesso di Microsoft Entra in Java, usare la libreria client delle identità di Azure.

Ecco un esempio dell'uso dell’identità di Azure per ottenere un token di accesso di Microsoft Entra da un browser:

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();

Il contesto del token deve essere impostato su "https://cognitiveservices.azure.com/.default".

Per ottenere un token di accesso di Microsoft Entra in Python, usare la libreria client delle identità di Azure.

Ecco un esempio dell'uso dell’identità di Azure per ottenere un token di accesso di Microsoft Entra da un browser interattivo:

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

Gli esempi di codice che consentono di ottenere un token di accesso di Microsoft Entra sono disponibili in Esempi di codice per Microsoft Identity Platform.

Per i linguaggi di programmazione in cui non è disponibile una libreria client di Microsoft Identity Platform, è possibile richiedere un token di accesso direttamente.

Ottenere l'ID della risorsa Voce

È necessario l'ID della risorsa Voce per effettuare chiamate SDK usando l'autenticazione di Microsoft Entra.

Nota

Per Riconoscimento finalità usare l'ID della risorsa di stima LUIS.

Per ottenere l'ID della risorsa nel portale di Azure:

  1. Passare al portale di Azure e accedere all'account Azure.
  2. Selezionare una risorsa Voce.
  3. Nel gruppo Gestione risorse nel riquadro sinistro, selezionare Proprietà.
  4. Copiare l'ID risorsa

Creare l'oggetto di configurazione di Speech SDK

Con un token di accesso di Microsoft Entra è ora possibile creare un oggetto di configurazione di Speech SDK.

Il metodo per fornire il token e il metodo per costruire l'oggetto Config di Speech SDK corrispondente variano in base all'oggetto usato.

SpeechRecognizer, SpeechSynthesizer, IntentRecognizer, ConversationTranscriber

Per gli oggetti SpeechRecognizer, SpeechSynthesizer, IntentRecognizer, ConversationTranscriber, creare il token di autorizzazione dall'ID della risorsa e dal token di accesso di Microsoft Entra e quindi usarlo per creare un oggetto SpeechConfig.

string resourceId = "Your Resource ID";
string aadToken = "Your Azure AD access token";
string region =  "Your Speech Region";

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

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and AAD 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 AAD 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 AAD access token.
authorizationToken = "aad#" + resourceId + "#" + aadToken.token
speechConfig = SpeechConfig(auth_token=authorizationToken, region=region)

TranslationRecognizer

Per TranslationRecognizer, creare il token di autorizzazione dall'ID della risorsa e dal token di accesso di Microsoft Entra e quindi usarlo per creare un oggetto SpeechTranslationConfig.

string resourceId = "Your Resource ID";
string aadToken = "Your Azure AD access token";
string region =  "Your Speech Region";

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

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and AAD 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 AAD 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 AAD access token.
authorizationToken = "aad#" + resourceId + "#" + aadToken.token
translationConfig = SpeechTranslationConfig(auth_token=authorizationToken, region=region)

DialogServiceConnector

Per l'oggetto DialogServiceConnection, creare il token di autorizzazione dall'ID della risorsa e dal token di accesso di Microsoft Entra e quindi usarlo per creare un oggetto CustomCommandsConfig o BotFrameworkConfig.

string resourceId = "Your Resource ID";
string aadToken = "Your Azure AD access token";
string region =  "Your Speech Region";
string appId = "Your app ID";

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and AAD access token.
var authorizationToken = $"aad#{resourceId}#{aadToken}";
var customCommandsConfig = CustomCommandsConfig.FromAuthorizationToken(appId, authorizationToken, region);
std::string resourceId = "Your Resource ID";
std::string aadToken = "Your Azure AD access token";
std::string region = "Your Speech Region";
std::string appId = "Your app Id";

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

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and AAD access token.
String authorizationToken = "aad#" + resourceId + "#" + token;
CustomCommandsConfig dialogServiceConfig = CustomCommandsConfig.fromAuthorizationToken(appId, authorizationToken, region);

Python attualmente non supporta DialogServiceConnector

VoiceProfileClient

Per usare VoiceProfileClient l’autenticazione di Microsoft Entra, usare il nome di dominio personalizzato creato in precedenza.

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

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

// You need to include the "aad#" prefix and the "#" (hash) separator between resource ID and AAD access token.
var authorizationToken = $"aad#{resourceId}#{aadToken}";
config.AuthorizationToken = authorizationToken;
std::string customDomainName = "Your Custom Name";
std::string aadToken = "Your Azure AD 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 AAD access token.
auto authorizationToken = "aad#" + resourceId + "#" + aadToken;
speechConfig->SetAuthorizationToken(authorizationToken);
String aadToken = "Your Azure AD 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 AAD access token.
String authorizationToken = "aad#" + resourceId + "#" + token;

speechConfig.setAuthorizationToken(authorizationToken);

VoiceProfileClient non è disponibile con Speech SDK per Python.

Nota

ConversationTranslator non supporta l'autenticazione di Microsoft Entra.