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

MacOS下通过dotnet-cli认证Azure DevOps私有NuGet包源遇401问题

解决MacOS上.NET项目访问Azure DevOps Artifacts私有源401未授权问题

核心问题说明

Azure DevOps Artifacts 不支持直接使用账号密码作为NuGet源凭据,必须使用个人访问令牌(PAT),且明文存储凭据存在安全风险,也可能导致MacOS系统凭据管理不兼容。


步骤1:生成Azure DevOps个人访问令牌(PAT)

  1. 登录Azure DevOps,进入用户设置 → 个人访问令牌
  2. 新建令牌,设置以下权限:
    • Packaging:勾选Read(若需要推送包则勾选Read & Write)
  3. 生成后复制令牌内容,务必保存好(刷新页面后无法再次查看)

步骤2:正确配置NuGet源凭据(推荐用系统钥匙串存储)

方式一:通过dotnet命令自动配置(推荐)

在项目根目录执行以下命令,直接将源和凭据存入MacOS钥匙串,避免明文泄露:

# 先移除已存在的同名源(如果之前配置过)
dotnet nuget remove source "Custom.Framework"

# 添加源并配置PAT凭据
dotnet nuget add source "https://pkgs.azure.com/example/nuget/v3/index.json" --name "Custom.Framework" --username pat --password <你的PAT内容>

注:用户名可以填任意值(比如pat),Azure DevOps实际验证的是PAT密码

方式二:手动修改nuget.config(不推荐明文存储)

如果必须手动修改配置,将ClearTextPassword替换为你的PAT,用户名可保留邮箱或填任意值:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="Custom.Framework" value="https://pkgs.azure.com/example/nuget/v3/index.json" />
  </packageSources>
  <packageSourceCredentials>
    <Custom.Framework>
        <add key="Username" value="exampleUsername@example.com" />
        <add key="ClearTextPassword" value="你的PAT内容" />
    </Custom.Framework>
  </packageSourceCredentials>
</configuration>

步骤3:验证VSCode C# DevKit凭据同步

  1. 在VSCode左侧打开Azure扩展面板,登录你的Azure DevOps账号
  2. 确保C# DevKit已识别到项目的NuGet源,可通过命令面板执行> .NET: Restore NuGet Packages触发恢复

步骤4:测试恢复命令

执行详细日志命令排查问题:

dotnet restore --verbosity detailed

如果仍报错,检查以下内容:

  • 源URL是否正确:确认example是你的Azure DevOps组织名称
  • PAT权限是否足够:确保Packaging权限已勾选
  • 代理设置:若Mac使用代理,需在nuget.config中添加代理配置:
    <config>
      <add key="http_proxy" value="http://你的代理地址:端口" />
      <add key="https_proxy" value="https://你的代理地址:端口" />
    </config>
    

内容的提问来源于stack exchange,提问作者Baransel DİNÇ

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 15:53:21