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

使用PowerShell执行Dotnet Build获取NuGet包时遇401错误

本地开发环境配置NuGet.org凭据拉取包的可行方案

首先明确:NuGet.org的公开包默认无需凭据即可拉取,若dotnet build拉取更新包失败,要么是你需要拉取需认证的私有包/受限包,要么是本地NuGet配置存在异常。以下是本地开发场景下的具体配置方法:

方式1:使用dotnet命令行快速配置

在PowerShell中执行以下命令,直接为NuGet.org源添加凭据:

  1. 先确认NuGet.org源已存在(不存在则添加):
    dotnet nuget add source https://api.nuget.org/v3/index.json --name nuget.org
    
  2. 配置凭据:
    • 若使用API密钥(NuGet.org官方推荐的认证方式):
      dotnet nuget update source nuget.org --username api --password 你的NuGet_API密钥 --store-password-in-clear-text
      
    • 若使用用户名密码认证(极少用于NuGet.org,多用于私有源):
      dotnet nuget update source nuget.org --username 你的用户名 --password 你的密码 --store-password-in-clear-text
      

注:--store-password-in-clear-text是本地开发的简便方案,若追求安全性,可改用Windows凭据管理器存储。

方式2:通过Windows凭据管理器安全存储凭据

将凭据存入系统凭据库,避免明文暴露:

  1. 打开「控制面板 > 用户账户 > 凭据管理器 > Windows凭据」
  2. 点击「添加普通凭据」
  3. 网络地址栏输入:https://api.nuget.org/v3/index.json
  4. 用户名填api(API密钥认证时固定值),密码填你的NuGet API密钥;若用用户名密码则填对应信息
  5. 保存后,dotnet build会自动读取该凭据完成拉取

方式3:手动编辑NuGet配置文件

本地NuGet配置文件路径为C:\Users\<你的用户名>\AppData\Roaming\NuGet\NuGet.Config,编辑该文件添加凭据节点:
找到<packageSources>下的nuget.org源,新增<packageSourceCredentials>配置:

<packageSources>
  <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
<packageSourceCredentials>
  <nuget.org>
    <add key="Username" value="api" />
    <add key="ClearTextPassword" value="你的NuGet_API密钥" />
  </nuget.org>
</packageSourceCredentials>

若不想明文存储密码,可使用nuget.exe生成加密密码,替换ClearTextPassword为Password并填入加密后的值。

常见问题排查

  • 校验源地址:确保使用的是https://api.nuget.org/v3/index.json,避免旧版v2地址导致的问题
  • 网络检查:确认本地可正常访问NuGet.org,可通过浏览器打开源地址验证
  • 清理缓存:执行dotnet nuget locals all --clear清除本地NuGet缓存后重新构建
  • 密钥有效性:登录NuGet.org后台检查API密钥是否未过期、权限是否覆盖所需包

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 18:32:32