Autentikasi Azure Active Directory dengan Speech SDK
Saat menggunakan Speech SDK untuk mengakses layanan Azure Cognitive Service untuk Ucapan, ada tiga metode autentikasi yang tersedia: kunci layanan, token berbasis kunci, dan Azure Active Directory (Azure AD). Artikel ini menjelaskan cara mengonfigurasi sumber daya Ucapan dan membuat objek konfigurasi Speech SDK supaya dapat menggunakan Azure AD untuk autentikasi.
Artikel ini menunjukkan cara menggunakan autentikasi Azure AD dengan Speech SDK. Anda akan mempelajari cara:
- Membuat sumber daya Ucapan
- Mengonfigurasi sumber daya Ucapan untuk autentikasi Azure AD
- Dapatkan token akses Azure AD
- Buat objek konfigurasi SDK yang sesuai.
Membuat sumber daya Ucapan
Untuk membuat sumber daya Ucapan di portal Microsoft Azure, lihat Mendapatkan kunci untuk sumber daya Anda
Mengonfigurasi sumber daya Ucapan untuk autentikasi Azure AD
Untuk mengonfigurasikan sumber daya Ucapan Anda untuk autentikasi Azure AD, buat nama domain kustom dan tetapkan peran.
Buat nama domain kustom
Ikuti langkah-langkah ini untuk membuat nama subdomain kustom untuk Cognitive Services untuk sumber daya Ucapan Anda.
Perhatian
Saat Anda mengaktifkan nama domain kustom, operasi tersebut tidak dapat dibatalkan. Satu-satunya cara untuk kembali ke nama regional adalah dengan membuat sumber Ucapan baru.
Jika sumber daya Ucapan Anda memiliki banyak model dan proyek kustom terkait yang dibuat melalui Speech Studio, sebaiknya coba konfigurasi dengan sumber daya pengujian sebelum Anda mengubah sumber daya yang digunakan dalam produksi.
Untuk membuat nama domain kustom dengan menggunakan portal Azure, ikuti langkah-langkah berikut:
Buka portal Azure dan masuk ke akun Azure Anda.
Pilih sumber Ucapan yang diperlukan.
Di grup Pengelolaan Sumber Daya di panel kiri, pilih Jaringan.
Pada tab Firewalls dan jaringan virtual, pilih Buat Nama Domain Kustom. Panel kanan baru muncul dengan petunjuk untuk membuat subdomain kustom yang unik untuk sumber daya Anda.
Di panel Buat Nama Domain Kustom, masukkan nama domain kustom. Domain kustom lengkap Anda akan terlihat seperti:
https://{your custom name}.cognitiveservices.azure.com.Ingatlah bahwa setelah Anda membuat nama domain kustom, nama itu tidak dapat diubah.
Setelah Anda memasukkan nama domain kustom, pilih Simpan.
Setelah operasi selesai, dalam grup Pengelolaan sumber daya, pilih Kunci dan Titik Akhir. Konfirmasikan bahwa nama titik akhir baru sumber daya Anda dimulai dengan cara ini:
https://{your custom name}.cognitiveservices.azure.com.
Menetapkan peran
Untuk autentikasi Azure AD dengan sumber daya Ucapan, Anda harus menetapkan Kontributor Ucapan Cognitive Services atau peran Pengguna Ucapan Cognitive Services.
Anda dapat menetapkan peran ke pengguna atau aplikasi menggunakan portal Azure atau PowerShell.
Dapatkan token akses Azure AD
Untuk mendapatkan token akses Azure AD di C#, gunakan Pustaka Klien Azure Identity.
Berikut adalah contoh penggunaan Azure Identity untuk mendapatkan token akses Azure AD dari browser interaktif:
TokenRequestContext context = new Azure.Core.TokenRequestContext(new string[] { "https://cognitiveservices.azure.com/.default" });
InteractiveBrowserCredential browserCredential = new InteractiveBrowserCredential();
var browserToken = browserCredential.GetToken(context);
Konteks token harus diatur ke "https://cognitiveservices.azure.com/.default".
Untuk mendapatkan token akses Azure AD di C++, gunakan Pustaka Klien Azure Identity.
Berikut adalah contoh penggunaan Azure Identity untuk mendapatkan token akses Azure AD dengan ID penyewa, ID klien, dan kredensial rahasia klien Anda:
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());
Konteks token harus diatur ke "https://cognitiveservices.azure.com/.default".
Untuk mendapatkan token akses Azure AD di Java, gunakan Pustaka Klien Azure Identity.
Berikut adalah contoh penggunaan Azure Identity untuk mendapatkan token akses Azure AD dari 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();
Konteks token harus diatur ke "https://cognitiveservices.azure.com/.default".
Untuk mendapatkan token akses Azure AD di Java, gunakan Pustaka Klien Azure Identity.
Berikut adalah contoh penggunaan Azure Identity untuk mendapatkan token akses Azure AD dari browser interaktif:
from azure.identity import InteractiveBrowserCredential
ibc = InteractiveBrowserCredential()
aadToken = ibc.get_token("https://cognitiveservices.azure.com/.default")
Temukan sampel yang mendapatkan token akses Azure AD di sampel kode platform identitas Microsoft.
Untuk bahasa pemrograman di mana pustaka klien platform identitas Microsoft tidak tersedia, Anda dapat langsung meminta token akses.
Dapatkan ID sumber daya Ucapan
Anda memerlukan ID sumber daya Ucapan untuk melakukan panggilan SDK menggunakan autentikasi Azure AD.
Catatan
Untuk Pengenalan Niat gunakan ID sumber daya Prediksi LUIS Anda.
Untuk mendapatkan ID sumber daya di portal Azure:
- Buka portal Azure dan masuk ke akun Azure Anda.
- Pilih sumber daya Ucapan.
- Di grup Pengelolaan Sumber Daya di panel kiri, pilih Properti.
- Salin ID Sumber Daya
Membuat objek konfigurasi Speech SDK
Dengan token akses Azure AD, Anda kini dapat membuat objek konfigurasi Speech SDK.
Metode penyediaan token, dan metode untuk membangun objek Config Speech SDK yang sesuai bervariasi bergantung pada objek yang akan Anda gunakan.
SpeechRecognizer, IntentRecognizer, ConversationTranscriber, dan SpeechSynthesizer
Untuk objek SpeechRecognizer, IntentRecognizer, ConversationTranscriber, dan SpeechSynthesizer, buat token otorisasi dari ID sumber daya dan token akses Azure AD, lalu gunakan token otorisasi tersebut untuk membuat objek 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
Untuk objek TranslationRecognizer, buat token otorisasi dari ID sumber daya dan token akses Azure AD, lalu gunakan token otorisasi tersebut untuk membuat objek 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
Untuk objek DialogServiceConnection, buat token otorisasi dari ID sumber daya dan token akses Azure AD, lalu gunakan token otorisasi tersebut untuk membuat objek CustomCommandsConfig atau 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 tidak didukung saat ini di Python
VoiceProfileClient
Untuk menggunakan VoiceProfileClient dengan autentikasi Azure AD, gunakan nama domain kustom yang dibuat di atas.
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 ini tidak tersedia dengan Speech SDK untuk Python.
Catatan
ConversationTranslator ini tidak mendukung autentikasi Azure AD.
