PublicKeyCredential: rawId 属性

Baseline 已广泛支持

此功能已成熟,并可在多种设备和浏览器版本上运行。自 2021 年 9 月起,所有浏览器均已支持此功能。

安全上下文: 此功能仅在安全上下文(HTTPS)中可用,且支持此功能的浏览器数量有限。

rawIdPublicKeyCredential 接口的一个只读属性,它是一个 ArrayBuffer 对象,包含凭据的标识符。

PublicKeyCredential.id 属性是此标识符的 base64url 编码版本。

注意: 此属性只能用于顶级上下文,在 <iframe> 中不可用。

一个 ArrayBuffer,包含凭据的标识符。该标识符预计是全局唯一的,并分配给当前的 PublicKeyCredential 及其相关的 AuthenticatorAssertionResponse

示例

js
const options = {
  challenge: new Uint8Array(26) /* from the server */,
  rp: {
    name: "Example CORP",
    id: "login.example.com",
  },
  user: {
    id: new Uint8Array(26) /* To be changed for each user */,
    name: "canand@example.com",
    displayName: "Carina Anand",
  },
  pubKeyCredParams: [
    {
      type: "public-key",
      alg: -7,
    },
  ],
};

navigator.credentials
  .create({ publicKey: options })
  .then((pubKeyCredential) => {
    const rawId = pubKeyCredential.rawId;
    // Do something with rawId
  })
  .catch((err) => {
    // Deal with any error
  });

规范

规范
Web Authentication:访问公钥凭证的 API - 第 3 级
# dom-publickeycredential-rawid

浏览器兼容性