You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WebAuthn开发:navigator.credentials.create()应在服务端还是客户端调用?

关于navigator.credentials.create()的调用位置解答

必须在客户端(前端)代码中调用navigator.credentials.create(),原因如下:

  1. 这个API是浏览器提供的WebAuthn核心接口,navigator是浏览器环境独有的全局对象,服务端运行环境(比如Node.js、Java后端等)不存在这个对象,根本无法执行该调用。

  2. 你看到的那段表述翻译后是:

服务端会通过在客户端触发调用navigator.credentials.create()来启动新凭证的创建流程。

这里的意思是流程由服务端发起:服务端会先生成创建凭证所需的配置参数,返回给客户端;但实际的API调用必须在客户端代码里执行,调用后再将生成的凭证数据回传给服务端保存。

举个简单的客户端代码示例:

// 浏览器端代码
async function initWebAuthnCreation() {
  // 从服务端获取创建凭证的配置参数
  const createOptions = await fetch('/get-webauthn-create-options')
    .then(res => res.json());
  
  // 调用浏览器的WebAuthn API生成凭证
  const newCredential = await navigator.credentials.create({
    publicKey: createOptions
  });
  
  // 将凭证发送给服务端存储
  await fetch('/save-webauthn-credential', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(newCredential)
  });
}

内容的提问来源于stack exchange,提问作者Justin Chan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 22:23:09