跳到主要内容
ecdsa.com
WebAuthn navigator.credentials.get() / create()WebAuthn 与通行密钥

错误信息

NotAllowedError: The operation either timed out or was not allowed. See: https://www.w3.org/TR/webauthn-2/#sctn-privacy-considerations-client.

也可能输出为

  • NotAllowedError: The operation either timed out or was not allowed.
  • NotAllowedError: CredentialsContainer request is not allowed.

上面是 Chromium 的文本(旧版本没有“See: …”链接)。Firefox 显示的是“CredentialsContainer request is not allowed.”(已在没有认证器的无头 Firefox 157 中复现)。消息因浏览器而异;只有 `e.name` 是固定的。

WebAuthn navigator.credentials.get() / create() — DOMException NotAllowedError (Chromium; Firefox wording in aliases)

含义

这是浏览器对 WebAuthn 失败给出的兜底错误。规范有意让网页无法区分“用户取消了”“已超时”“没有认证器持有匹配的凭证”和“该请求在当前上下文中不被允许”,这样网站就无法探测设备上存在哪些凭证(链接中的隐私章节解释了原因)。因此,你的代码只能把它当作“没有产生凭证”,并提供其他途径;真正的原因要靠复现来查找,而不是靠阅读错误信息。

原因

修复方法

  1. 1.

    按“没有凭证”处理,并提供替代方案

    不要显示吓人的错误信息。提供其他登录方式、换用另一台设备(混合/QR 码流程),或者注册新的通行密钥。

    js
    try {
      const credential = await navigator.credentials.get({ publicKey: options });
      await finishLogin(credential);
    } catch (e) {
      if (e.name === "NotAllowedError") {
        // Cancelled, timed out, or no passkey for this account on this device: offer alternatives.
        showOtherSignInOptions();
      } else {
        throw e;
      }
    }
  2. 2.

    注册时提供常用算法

    列出 ES256、EdDSA 和 RS256 几乎就能覆盖所有认证器;相应地,你的服务器必须能验证这三种签名。

    js
    // Registration: offer the algorithms authenticators actually support.
    pubKeyCredParams: [
      { type: "public-key", alg: -7 },    // ES256
      { type: "public-key", alg: -8 },    // EdDSA (Ed25519)
      { type: "public-key", alg: -257 },  // RS256 (Windows Hello and older security keys)
    ],

相关错误

浏览完整的签名错误库