適用于Visual Studio Code驗證的 Azure 身分識別外掛程式
此套件提供適用于 JavaScript 的 Azure 身分識別程式庫外掛程式 () @azure/identity
,可透過適用于 Visual Studio Code 的「Azure 帳戶」擴充功能進行驗證。 此外掛程式提供 中的 @azure/identity
相 VisualStudioCodeCredential
依性,並讓它本身或 作為 的一 DefaultAzureCredential
部分使用。
| 原始程式碼樣品
開始使用
import { useIdentityPlugin } from "@azure/identity";
import { vsCodePlugin } from "@azure/identity-vscode";
useIdentityPlugin(vsCodePlugin);
Prerequisites
安裝套件
此套件的設計目的是要與適用于 JavaScript 的 Azure 身分識別搭配使用。 @azure/identity
使用 npm
安裝 和 此套件:
$ npm install --save @azure/identity
$ npm install --save-dev @azure/identity-vscode
支援的環境
適用于 JavaScript 的 Azure 身分識別外掛程式支援穩定 (甚至從 v12 開始編號Node.js版本的) 版本。 雖然外掛程式可能會在其他 Node 版本中執行,但不會保證支援。 @azure/identity-vscode
不支援 瀏覽器環境。
重要概念
如果這是您第一次使用 @azure/identity
或 Microsoft 身分識別平台 (Azure Active Directory),建議您先參閱使用 @azure/identity
Microsoft 身分識別平台。 此文件可幫助您深入瞭解平台及如何正確設定 Azure 帳戶。
Azure 身分識別外掛程式
@azure/identity
從 2.0.0 版起,適用于 JavaScript 的身分識別用戶端程式庫包含外掛程式 API。 此套件 (@azure/identity-vscode
) 匯出外掛程式物件,您必須從封裝傳遞為最上層 useIdentityPlugin
函式的 @azure/identity
引數。 透過Visual Studio Code的「Azure 帳戶」擴充功能啟用驗證,如下所示:
import { useIdentityPlugin } from "@azure/identity";
import { vsCodePlugin } from "@azure/identity-vscode";
useIdentityPlugin(vsCodePlugin);
呼叫 useIdentityPlugin
之後, VisualStudioCodeCredential
將會啟用封裝中的 @azure/identity
。 如果未使用此外掛程式,則會 VisualStudioCodeCredential
擲回 CredentialUnavailableError
,而且不會作為 的 DefaultAzureCredential
一部分使用。
Visual Studio Code驗證
VisualStudioCodeCredential
會使用 來自「Azure 帳戶」擴充功能的驗證會話。 若要使用此認證,您必須使用擴充功能登入您的 Azure 帳戶。 若要這樣做,請開啟Visual Studio Code,確定已安裝擴充功能,並使用 [Azure: 登入] 選項從命令選擇區登入,以開啟瀏覽器視窗並登入 Azure。 或者,您也可以選取 [Azure:使用裝置程式碼登入] 以使用裝置程式碼流程。
登入之後,您可能需要選取訂用帳戶 (例如,如果您有多個 Azure 訂用帳戶) ,而且您可以使用功能表來變更使用中的訂用帳戶,以選取 [Azure: 選取訂用帳戶] 專案。
範例
註冊外掛程式之後,您可以使用 VisualStudioCodeCredential
與 中的 @azure/identity
其他認證類別類似的方式:
import { useIdentityPlugin, VisualStudioCodeCredential } from "@azure/identity";
import { vsCodePlugin } from "@azure/identity-vscode";
useIdentityPlugin(vsCodePlugin);
async function main() {
const credential = new VisualStudioCodeCredential();
// The graph.microsoft.com scope is used as an example
const scope = "https://graph.microsoft.com/.default";
// Print out part of the access token
console.log((await credential.getToken(scope)).token.substr(0, 10), "...");
}
main().catch((error) => {
console.error("An error occurred:", error);
process.exit(1);
});
您也可以使用 DefaultAzureCredential
,如果可以使用,它將會嘗試使用「Azure 帳戶」擴充功能進行驗證,以進行Visual Studio Code:
import { useIdentityPlugin, DefaultAzureCredential } from "@azure/identity";
import { vsCodePlugin } from "@azure/identity-vscode";
useIdentityPlugin(vsCodePlugin);
async function main() {
// With the plugin enabled above, `DefaultAzureCredential` will use
// Visual Studio Code's "Azure Account" extension to authenticate if
// it is available.
const credential = new DefaultAzureCredential();
// This will print a JWT access_token and its expiration timestamp
// The graph.microsoft.com scope is used as an example
console.log("Token:", await credential.getToken("https://graph.microsoft.com/.default"));
}
main().catch((error) => {
console.error("An error occurred:", error);
process.exit(1);
});
疑難排解
記錄
啟用記錄有助於找出失敗的相關實用資訊。 若要查看 HTTP 的要求和回應記錄,請將 AZURE_LOG_LEVEL
環境變數設定為 info
。 或者,您可以在 @azure/logger
中呼叫 setLogLevel
,以在執行階段啟用記錄:
import { setLogLevel } from "@azure/logger";
setLogLevel("info");
後續步驟
提供意見反應
如果您遇到錯誤或有任何建議,請提出問題。
參與
如果您希望向此程式庫投稿,請參閱投稿指南,深入瞭解如何組建與測試程式碼。
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應