PublicKeyCredential:getClientExtensionResults() 方法

Baseline 已廣泛支援

此功能已成熟,並可在多種裝置和瀏覽器版本上執行。自 2021 年 9 月起,所有瀏覽器均已支援此功能。

安全上下文: 此功能僅在安全上下文(HTTPS)中可用,且支援此功能的瀏覽器數量有限。

PublicKeyCredential 介面的 getClientExtensionResults() 方法返回一個物件,該物件將憑據建立或身份驗證期間請求的擴充套件的識別符號對映到它們被使用者代理處理後的結果。

在建立或獲取 PublicKeyCredential(分別透過 navigator.credentials.create()navigator.credentials.get())時,可以透過 publicKey 選項的 extensions 屬性請求客戶端進行“自定義”處理不同的擴充套件。有關請求不同擴充套件的更多資訊,請參閱 Web 身份驗證擴充套件

注意: getClientExtensionResults() 僅返回由使用者代理(客戶端)處理的擴充套件的結果。由身份驗證器處理的擴充套件的結果可以在 身份驗證器資料 中找到,該資料可在 AuthenticatorAssertionResponse.authenticatorData 中獲取。

語法

js
getClientExtensionResults()

引數

無。

返回值

一個物件,每個條目是擴充套件的識別符號字串作為鍵,以及客戶端處理擴充套件的輸出作為值。

異常

SecurityError DOMException

RP 域無效。

示例

js
const publicKey = {
  // Here are the extension "inputs"
  extensions: {
    appid: "https://accounts.example.com",
  },
  allowCredentials: {
    id: "fgrt46jfgd...",
    transports: ["usb", "nfc"],
    type: "public-key",
  },
  challenge: new Uint8Array(16) /* from the server */,
};

navigator.credentials
  .get({ publicKey })
  .then((publicKeyCred) => {
    const myResults = publicKeyCred.getClientExtensionResults();
    // myResults will contain the output of processing the "appid" extension
  })
  .catch((err) => {
    console.error(err);
  });

規範

規範
Web Authentication:訪問公鑰憑證的 API - 第 3 級
# dom-publickeycredential-getclientextensionresults

瀏覽器相容性

注意: 擴充套件是可選的,不同的瀏覽器可能識別不同的擴充套件。客戶端始終可以忽略處理擴充套件:如果瀏覽器不識別給定的擴充套件,它將直接忽略它。有關哪些瀏覽器支援哪些擴充套件的資訊,請參閱 Web 身份驗證擴充套件

另見