Authentification Microsoft Entra avec le kit de développement logiciel (SDK) Speech

Lorsque vous utilisez le kit de développement logiciel (SDK) Speech pour accéder au service Speech, trois méthodes d’authentification sont disponibles : les clés de service, un jeton basé sur une clé et Microsoft Entra ID. Cet article explique comment configurer une ressource Speech et créer un objet de configuration du kit de développement logiciel (SDK) Speech afin d’utiliser Microsoft Entra ID pour l’authentification.

Cet article montre comment utiliser l’authentification Microsoft Entra avec le kit de développement logiciel (SDK) Speech. Vous allez apprendre à effectuer les actions suivantes :

  • Créer une ressource Speech
  • Configurer la ressource Speech pour l’authentification Microsoft Entra
  • Obtenir un jeton d’accès Microsoft Entra
  • Créer l’objet de configuration du SDK approprié

Pour en savoir plus sur les jetons d’accès Microsoft Entra, notamment leur durée de vie, consultez Jetons d’accès de la plateforme d’identités Microsoft.

Créer une ressource Speech

Pour créer une ressource Speech dans le Portail Azure, consultez ce démarrage rapide.

Configurer la ressource Speech pour l’authentification Microsoft Entra

Pour configurer votre ressource Speech pour l’authentification Microsoft Entra, créez un nom de domaine personnalisé et attribuez des rôles.

Créer un nom de domaine personnalisé

Effectuez ces étapes afin de créer un nom de sous-domaine personnalisé pour Azure AI Services pour votre ressource Speech.

Attention

Lorsque vous activez un nom de domaine personnalisé, l’opération n’est pas réversible. La seule façon de revenir au nom régional consiste à créer une ressource Speech.

Si votre ressource Speech comporte un grand nombre de modèles et de projets personnalisés associés créés avec Speech Studio, nous vous recommandons vivement d’essayer la configuration avec une ressource test avant de modifier la ressource utilisée en production.

Pour créer un nom de domaine personnalisé à l’aide du portail Azure, effectuez les étapes suivantes :

  1. Accédez au portail Azure et connectez-vous à votre compte Azure.

  2. Sélectionnez la ressource Speech requise.

  3. Dans le groupe Gestion des ressources dans le volet gauche, sélectionnez Réseau.

  4. Sous l’onglet Pare-feux et réseaux virtuels, sélectionnez Générer un nom de domaine personnalisé. Cela a pour effet d’ouvrir un nouveau panneau contenant des instructions pour créer un sous-domaine personnalisé unique pour votre ressource.

  5. Dans le panneau Générer un nom de domaine personnalisé, entrez un nom de domaine personnalisé. Votre domaine personnalisé complet ressemble à ce qui suit : https://{your custom name}.cognitiveservices.azure.com.

    N’oubliez pas : après avoir créé un nom de domaine personnalisé, vous ne pouvez plus le modifier.

    Une fois que vous avez entré votre nom de domaine personnalisé, sélectionnez Enregistrer.

  6. Quand l’opération est terminée, dans le groupe Gestion des ressources, sélectionnez Clés et point de terminaison. Confirmez que le nouveau nom de point de terminaison de votre ressource commence ainsi : https://{your custom name}.cognitiveservices.azure.com.

Attribuer des rôles

Pour l’authentification Microsoft Entra avec des ressources Speech, vous devez affecter le rôle de Contributeur Speech Azure Cognitive Services ou d’Utilisateur Speech Azure Cognitive Services.

Vous pouvez assigner des rôles à l’utilisateur ou à l’application à l’aide du portail Azure ou de PowerShell.

Obtenir un jeton d’accès Microsoft Entra

Pour obtenir un jeton d’accès Microsoft Entra dans C#, utilisez la Bibliothèque de client Azure Identity.

Voici un exemple d’utilisation d’Azure Identity pour obtenir un jeton d’accès Microsoft Entra à partir d’un navigateur interactif :

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;

Le contexte du jeton doit avoir la valeur "https://cognitiveservices.azure.com/.default".

Pour obtenir un jeton d’accès Microsoft Entra dans C++, utilisez la Bibliothèque de client Azure Identity.

Voici un exemple d’utilisation d’Azure Identity pour obtenir un jeton d’accès Microsoft Entra avec votre ID de locataire, ID client et les informations d’identification de la clé secrète 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());

Le contexte du jeton doit avoir la valeur "https://cognitiveservices.azure.com/.default".

Pour obtenir un jeton d’accès Microsoft Entra dans Java, utilisez la Bibliothèque de client Azure Identity.

Voici un exemple d’utilisation d’Azure Identity pour obtenir un jeton d’accès Microsoft Entra à partir d’un navigateur :

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

Le contexte du jeton doit avoir la valeur "https://cognitiveservices.azure.com/.default".

Pour obtenir un jeton d’accès Microsoft Entra dans Python, utilisez la Bibliothèque de client Azure Identity.

Voici un exemple d’utilisation d’Azure Identity pour obtenir un jeton d’accès Microsoft Entra à partir d’un navigateur interactif :

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

Vous trouverez des exemples qui obtiennent un jeton d’accès Microsoft Entra dans Exemples de code pour la plateforme d’identités Microsoft.

Pour les langages de programmation dans lesquels aucune bibliothèque de client de plateforme d’identités Microsoft n’est disponible, vous pouvez demander directement un jeton d’accès.

Obtenir l’ID de ressource Speech

Vous avez besoin de votre ID de ressource Speech pour effectuer des appels de kit de développement logiciel (SDK) à l’aide de l’authentification Microsoft Entra.

Remarque

Pour Reconnaissance de l’intention, utilisez votre ID de ressource de prédiction LUIS.

Pour récupérer l’ID de ressource dans le portail Azure :

  1. Accédez au portail Azure et connectez-vous à votre compte Azure.
  2. Sélectionnez une ressource Speech.
  3. Dans le groupe Gestion des ressources dans le volet gauche, sélectionnez Propriétés.
  4. Copiez l’ID de ressource.

Créer l’objet de configuration du SDK Speech

Avec un jeton d’accès Microsoft Entra, vous pouvez désormais créer un objet de configuration du kit de développement logiciel (SDK) Speech.

La méthode de fourniture du jeton, ainsi que la méthode permettant de construire l’objet Config du Kit de développement logiciel (SDK) Speech correspondant, varient en fonction de l’objet que vous utilisez.

SpeechRecognizer, SpeechSynthesizer, IntentRecognizer, ConversationTranscriber

Pour les objets SpeechRecognizer, SpeechSynthesizer, IntentRecognizer et ConversationTranscriber, générez le jeton d’autorisation à partir de l’ID de ressource et du jeton d’accès Microsoft Entra, puis utilisez-le pour créer un objet 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

Pour TranslationRecognizer, générez le jeton d’autorisation à partir de l’ID de ressource et du jeton d’accès Microsoft Entra, puis utilisez-le pour créer un objet 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

Pour l’objet DialogServiceConnection, générez le jeton d’autorisation à partir de l’ID de ressource et du jeton d’accès Microsoft Entra, puis utilisez-le pour créer un objet CustomCommandsConfig ou 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);

DialogServiceConnector n’est actuellement pas pris en charge en Python

VoiceProfileClient

Pour utiliser VoiceProfileClient avec l’authentification Microsoft Entra, utilisez le nom de domaine personnalisé créé ci-dessus.

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 n’est pas disponible avec le SDK Speech pour Python.

Remarque

ConversationTranslator ne prend pas en charge l’authentification Microsoft Entra.