@azure/identity package
類別
AggregateAuthenticationError |
提供陣列, |
AuthenticationError |
提供有關使用 Azure Active Directory 進行驗證失敗的詳細數據。 欄位 |
AuthenticationRequiredError |
嘗試以無訊息方式擷取令牌之後,用來強制執行驗證的錯誤。 |
AuthorizationCodeCredential |
啟用驗證以使用透過授權碼流程取得的授權碼來 Microsoft Entra ID,如 Microsoft Entra ID 檔中詳細說明: https://learn.microsoft.com/entra/identity-platform/v2-oauth2-auth-code-flow |
AzureCliCredential |
此認證會透過 Azure CLI (『az』) 命令行工具使用目前登入的使用者登入資訊。 若要這樣做,它會讀取使用者存取令牌,並使用 Azure CLI 命令 “az account get-access-token” 到期時間。 |
AzureDeveloperCliCredential |
Azure Developer CLI 是命令行介面工具,可讓開發人員在 Azure 中建立、管理及部署資源。 其建置在 Azure CLI 之上,並提供 Azure 開發人員特有的其他功能。 它可讓使用者以使用者身分進行驗證,以及/或服務主體,以 Microsoft Entra ID 進行驗證。 AzureDeveloperCliCredential 會在開發環境中進行驗證,並代表 Azure Developer CLI 中登入的使用者或服務主體取得令牌。 它會作為登入使用者或服務主體的 Azure Developer CLI,並在下方執行 Azure CLI 命令,以針對 Microsoft Entra ID 驗證應用程式。 設定 AzureDeveloperCliCredential若要使用此認證,開發人員必須使用下列其中一個命令,在本機 Azure Developer CLI 進行驗證:
您可能需要在特定時段後重複此程式,視組織中的重新整理令牌有效性而定。 一般而言,重新整理令牌有效期間是數周到幾個月。 AzureDeveloperCliCredential 會提示您再次登入。 |
AzurePowerShellCredential |
此認證將使用 Azure PowerShell 模組中目前登入的用戶資訊。 若要這樣做,它會使用 Azure PowerShell 命令讀取使用者存取令牌並到期時間 |
ChainedTokenCredential |
允許嘗試多個 |
ClientAssertionCredential |
使用 JWT 判斷提示驗證服務主體。 |
ClientCertificateCredential |
啟用驗證以使用指派給應用程式註冊的 PEM 編碼憑證來 Microsoft Entra ID。 如需如何設定憑證驗證的詳細資訊,請參閱這裡: |
ClientSecretCredential |
啟用驗證,以使用為應用程式註冊產生的用戶端密碼 Microsoft Entra ID。 如需如何設定用戶端密碼的詳細資訊,請參閱這裡: |
CredentialUnavailableError |
這表示在鏈結認證中嘗試的認證無法當做認證使用。 它不會將此視為應該停止鏈結的錯誤,而是攔截到該鏈結,而鏈結會繼續 |
DefaultAzureCredential |
提供預設 的 ChainedTokenCredential 組態,適用於大部分使用 Azure SDK 的應用程式。 |
DeviceCodeCredential |
啟用驗證以使用用戶可輸入https://microsoft.com/devicelogin的裝置代碼 Microsoft Entra ID。 |
EnvironmentCredential |
啟用驗證以使用用戶端密碼或憑證 Microsoft Entra ID,或使用使用者名稱和密碼作為使用者。 |
InteractiveBrowserCredential |
啟用使用互動式登入流程在網頁瀏覽器中 Microsoft Entra ID 驗證。 |
ManagedIdentityCredential |
嘗試使用部署環境中可用的受控識別進行驗證。 此驗證類型適用於 Azure VM、App Service 實例、Azure Functions 應用程式、Azure Kubernetes Services、Azure Service Fabric 實例和 Azure Cloud Shell 內部。 如需設定受控識別的詳細資訊,請參閱這裡: https://learn.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/overview |
OnBehalfOfCredential |
啟用驗證以使用代表流程 Microsoft Entra ID。 |
UsernamePasswordCredential |
啟用驗證以使用者的使用者名稱和密碼 Microsoft Entra ID。 此認證需要高度信任,因此您只能在無法使用其他更安全的認證類型時使用它。 |
VisualStudioCodeCredential |
使用 VSCode 擴充功能 'Azure 帳戶' 所提供的認證連線到 Azure。 一旦使用者透過延伸模組登入,此認證就可以共用擴充功能快取的相同重新整理令牌。 此認證不適用於 0.9.11 之前的 Azure 帳戶擴充功能版本。 此問題的長期修正正在進行中。 同時,請考慮使用 AzureCliCredential 進行驗證。 |
WorkloadIdentityCredential |
工作負載身分識別驗證是 Azure 中的一項功能,可讓在虛擬機上執行的應用程式 (VM) 存取其他 Azure 資源,而不需要服務主體或受控識別。 使用工作負載身分識別驗證,應用程式會使用自己的身分識別自行驗證,而不是使用共用服務主體或受控識別。 在幕後,工作負載身分識別驗證會使用服務帳戶認證 () 的概念,這些認證是由 Azure 自動建立,並安全地儲存在 VM 中。 藉由使用工作負載身分識別驗證,您可以避免需要針對每個 VM 上的每個應用程式管理和輪替服務主體或受控識別。 此外,因為 SA 會自動建立並由 Azure 管理,所以您不需要擔心儲存和保護敏感性認證本身。 WorkloadIdentityCredential 支援在 Azure Kubernetes 上 Microsoft Entra 工作負載 ID 驗證,並使用 Azure Kubernetes 環境中提供的 SAC 來取得令牌。 如需詳細資訊,請參閱 Microsoft Entra 工作負載 ID。 |
介面
類型別名
BrokerOptions |
在 InteractiveBrowserCredential 中啟用 WAM 訊息代理程式驗證的參數。 |
BrowserLoginStyle |
(僅限瀏覽器的功能) 驗證流程中要使用的「登入樣式」:
|
ClientCertificateCredentialPEMConfiguration |
ClientCertificateCredential 的必要組態選項,包含 PEM 憑證的字串內容,或 PEM 憑證的路徑。 |
DeviceCodePromptCallback |
定義將傳遞至 DeviceCodeCredential 的回呼簽章,以便向使用者顯示驗證詳細數據。 |
IdentityPlugin |
Azure 身分識別外掛程式的類型,這是接受外掛程式內容的函式。 |
OnBehalfOfCredentialOptions |
OnBehalfOfCredential 類別的選擇性參數。 |
列舉
AzureAuthorityHosts |
已知 Azure 授權單位主機的清單 |
函數
deserialize |
將先前串行化的驗證記錄從字串還原串行化為物件。 輸入字串必須包含下列屬性:
如果不支持我們收到的版本,將會擲回錯誤。 目前唯一可用的版本是:“1.0”,一律會在驗證記錄串行化時設定。 |
get |
傳回提供持有人令牌的回呼。 例如,持有人令牌可用來驗證要求,如下所示:
|
get |
傳回 DefaultAzureCredential 的新實例。 |
serialize |
將 序列化驗證記錄的輸出將包含下列屬性:
若要稍後將此字串轉換成串列化 |
use |
使用其他功能擴充 Azure 身分識別。 從外掛程式套件傳遞外掛程式,例如:
範例:
|
函式詳細資料
deserializeAuthenticationRecord(string)
將先前串行化的驗證記錄從字串還原串行化為物件。
輸入字串必須包含下列屬性:
- “authority”
- “homeAccountId”
- “clientId”
- “tenantId”
- “username”
- “version”
如果不支持我們收到的版本,將會擲回錯誤。
目前唯一可用的版本是:“1.0”,一律會在驗證記錄串行化時設定。
function deserializeAuthenticationRecord(serializedRecord: string): AuthenticationRecord
參數
- serializedRecord
-
string
先前串行化為字串的驗證記錄。
傳回
AuthenticationRecord。
getBearerTokenProvider(TokenCredential, string | string[], GetBearerTokenProviderOptions)
傳回提供持有人令牌的回呼。 例如,持有人令牌可用來驗證要求,如下所示:
import { DefaultAzureCredential } from "@azure/identity";
const credential = new DefaultAzureCredential();
const scope = "https://cognitiveservices.azure.com/.default";
const getAccessToken = getBearerTokenProvider(credential, scope);
const token = await getAccessToken();
// usage
const request = createPipelineRequest({ url: "https://example.com" });
request.headers.set("Authorization", `Bearer ${token}`);
function getBearerTokenProvider(credential: TokenCredential, scopes: string | string[], options?: GetBearerTokenProviderOptions): () => Promise<string>
參數
- credential
- TokenCredential
用來驗證要求的認證。
- scopes
-
string | string[]
持有人令牌所需的範圍。
- options
- GetBearerTokenProviderOptions
設定令牌提供者的選項。
傳回
() => Promise<string>
提供持有人令牌的回呼。
getDefaultAzureCredential()
serializeAuthenticationRecord(AuthenticationRecord)
將 AuthenticationRecord
串行化為字串。
序列化驗證記錄的輸出將包含下列屬性:
- “authority”
- “homeAccountId”
- “clientId”
- “tenantId”
- “username”
- “version”
若要稍後將此字串轉換成串列化 AuthenticationRecord
,請使用匯出的函式 deserializeAuthenticationRecord()
。
function serializeAuthenticationRecord(record: AuthenticationRecord): string
參數
- record
- AuthenticationRecord
傳回
string
useIdentityPlugin(IdentityPlugin)
使用其他功能擴充 Azure 身分識別。 從外掛程式套件傳遞外掛程式,例如:
@azure/identity-cache-persistence
:提供永續性令牌快取@azure/identity-vscode
:提供 的VisualStudioCodeCredential
相依性,並加以啟用
範例:
import { cachePersistencePlugin } from "@azure/identity-cache-persistence";
import { useIdentityPlugin, DefaultAzureCredential } from "@azure/identity";
useIdentityPlugin(cachePersistencePlugin);
// The plugin has the capability to extend `DefaultAzureCredential` and to
// add middleware to the underlying credentials, such as persistence.
const credential = new DefaultAzureCredential({
tokenCachePersistenceOptions: {
enabled: true
}
});
function useIdentityPlugin(plugin: IdentityPlugin)
參數
- plugin
- IdentityPlugin
要註冊的外掛程式