大网数据广告活动
Web 标准与前端开发

PublicKeyCredential

· 52CE 我爱测

The **`PublicKeyCredential`** interface provides information about a public key / private key pair, which is a credential for logging in to a service using an un-phishable and data-breach resistant asymmetric key pair instead of a password. It inherits from Credential, and is par

Web Authentication API

The PublicKeyCredential interface provides information about a public key / private key pair, which is a credential for logging in to a service using an un-phishable and data-breach resistant asymmetric key pair instead of a password. It inherits from Credential, and is part of the Web Authentication API extension to the Credential Management API.

[!NOTE] This API is restricted to top-level contexts. Use from within an iframe element will not have any effect.

Instance properties

  • PublicKeyCredential.authenticatorAttachment

    • : A string that indicates the mechanism by which the WebAuthn implementation is attached to the authenticator at the time the associated CredentialsContainer.create() or CredentialsContainer.get() call completes.
  • PublicKeyCredential.id

    • : Inherited from Credential and overridden to be the base64url encoding of PublicKeyCredential.rawId.
  • PublicKeyCredential.rawId

    • : An ArrayBuffer that holds the globally unique identifier for this PublicKeyCredential. This identifier can be used to look up credentials for future calls to CredentialsContainer.get().
  • PublicKeyCredential.response

    • : An instance of an AuthenticatorResponse object. It is either of type AuthenticatorAttestationResponse if the PublicKeyCredential was the results of a CredentialsContainer.create() call, or of type AuthenticatorAssertionResponse if the PublicKeyCredential was the result of a CredentialsContainer.get() call.
  • PublicKeyCredential.type

    • : Inherited from Credential. Always set to public-key for PublicKeyCredential instances.

Static methods

  • PublicKeyCredential.getClientCapabilities_static
    • : Returns a Promise that resolves with an object that can be used to check whether or not particular WebAuthn capabilities and extensions are supported.
  • PublicKeyCredential.isConditionalMediationAvailable_static
    • : Returns a Promise which resolves to true if conditional mediation is available.
  • PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable_static
    • : Returns a Promise which resolves to true if an authenticator bound to the platform is capable of verifying the user.
  • PublicKeyCredential.parseCreationOptionsFromJSON_static
  • PublicKeyCredential.parseRequestOptionsFromJSON_static
  • PublicKeyCredential.signalAllAcceptedCredentials_static
  • PublicKeyCredential.signalCurrentUserDetails_static
    • : Signals to the authenticator that a particular user has updated their user name and/or display name.
  • PublicKeyCredential.signalUnknownCredential_static
    • : Signals to the authenticator that a credential ID was not recognized by the relying party server, for example because it was deleted.

Instance methods

  • PublicKeyCredential.getClientExtensionResults()
    • : If any extensions were requested, this method will return the results of processing those extensions.
  • PublicKeyCredential.toJSON()
    • : Returns a JSON-serializable plain object representing the PublicKeyCredential object. Automatically called by JSON.stringify().

Examples

Creating a new instance of PublicKeyCredential

Here, we use CredentialsContainer.create() to generate a new credential.

const createCredentialOptions = {
  publicKey: {
    challenge: new Uint8Array([
      21, 31, 105 /* 29 more random bytes generated by the server */,
    ]),
    rp: {
      name: "Example CORP",
      id: "login.example.com",
    },
    user: {
      id: new Uint8Array(16),
      name: "canand@example.com",
      displayName: "Carina Anand",
    },
    pubKeyCredParams: [
      {
        type: "public-key",
        alg: -7,
      },
    ],
  },
};

navigator.credentials
  .create(createCredentialOptions)
  .then((newCredentialInfo) => {
    const response = newCredentialInfo.response;
    const clientExtensionsResults =
      newCredentialInfo.getClientExtensionResults();
  })
  .catch((err) => {
    console.error(err);
  });

Getting an existing instance of PublicKeyCredential

Here, we fetch an existing credential from an authenticator, using CredentialsContainer.get().

const requestCredentialOptions = {
  publicKey: {
    challenge: new Uint8Array([/* bytes sent from the server */]),
  },
};

navigator.credentials
  .get(requestCredentialOptions)
  .then((credentialInfoAssertion) => {
    // send assertion response back to the server
    // to proceed with the control of the credential
  })
  .catch((err) => {
    console.error(err);
  });

Specifications

Browser compatibility

See also

  • The parent interface Credential

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。