WebAuthn:无法创建公钥。承诺被拒绝

xHo*_*dek 2 javascript authentication web webauthn

我正在尝试在我们的登录页面上设置 WebAuthn。我现在需要使用 来生成公钥navigator.credentials.create()。在 Chrome 上,我不断收到以下错误:Uncaught (in promise) DOMException: The operation either timed out or was not allowed. See: https://www.w3.org/TR/webauthn-2/#sctn-privacy-considerations-client.

这是相关代码:

if (isAvailable) {
    // Get challenge from server
    fetch("WebAuthn/WebAuthn.ashx", {
        method: "POST"
    })
    .then(res => res.json())
    .then(res => {
        const publicKeyCredentialCreationOptions = {
            challenge: Uint8Array.from(
                res.data, c => c.charCodeAt(0)),
            rp: {
                id: "localhost",
                name: "Company Name"
            },
            authenticatorSelection: {
                authenticatorAttachment: "platform",
                userVerification: "discouraged"
            },
            pubKeyCredParams: [{alg: -7, type: "public-key"}],
            user: {
                id: Uint8Array.from(
                    "UZSL85T9AFC", c => c.charCodeAt(0)),
                displayName: "User",
                name: document.getElementById("tbUser").value // taken from aspx form
            }
        };

        const credential = navigator.credentials.create({
            publicKey: publicKeyCredentialCreationOptions
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

一些可能有用的附加信息:

  • 服务器尚未处理此信息,但我认为这并不重要,因为需要在发送凭据之前创建凭据
  • 目前正在 https://localhost 上测试
  • 这是在用户登录之前在登录页面上发生的。这个想法是在用户点击提交后提示用户

Aar*_*ong 5

首先,您可以在 Chrome 上使用虚拟身份验证器进行测试,如下图所示。

在此输入图像描述

在 Windows 上,您可以将 Windows Hello 设置为身份验证器并稍后进行测试。

现在针对您的问题提供一些注释

  1. 本地主机不需要 https
  2. 如果仅使用 http,则指定预期来源,可以是 http://localhost<:port if not 80>
  3. 需要检查发送的格式,是否为arraybyte

我已经成功让它工作了...你可以尝试看看我的示例代码并使用它们,只有 2 个文件

我仍在清理它以使其成为生产代码(例如,在前端和后端之间传递信息时使用 JWT / Sessions)。