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

使用dotnet restore访问Azure DevOps NuGet源遇NU1301错误求助

解决dotnet restore无法访问Azure DevOps NuGet源的问题

问题重现

执行dotnet restore --interactive时出现以下错误:

dotnet restore --interactive
  Determining projects to restore...
C:\Users\...\MyProject.UnitTests.csproj : error NU1301: Unable to load the service index for source https://pkgs.dev.azure.com/.../nuget/v3/index.json. [C:\Users\..\MyProject.sln]
  Failed to restore C:\Users\MyProject.UnitTests.csproj (in 6.85 sec).
  4 of 5 projects are up-to-date for restore.

Visual Studio中可正常还原NuGet包,但命令行模式下失败,--interactive选项及凭据管理器均未解决问题,需实现PowerShell批量构建的自动还原步骤。

解决方法

1. 手动配置Azure DevOps NuGet源凭据

  • 生成Azure DevOps个人访问令牌(PAT):确保令牌拥有**包读取(Packaging Read)**权限,在Azure DevOps的「用户设置」→「个人访问令牌」中创建。
  • 执行以下dotnet命令添加源并绑定凭据:
    dotnet nuget add source https://pkgs.dev.azure.com/[你的组织名]/[你的项目名]/nuget/v3/index.json --name "AzureDevOps_NuGet" --username any --password [你的PAT] --store-password-in-clear-text
    
    注:--store-password-in-clear-text用于绕过Windows凭据管理器的潜在问题,若后续凭据管理器生效,可移除该参数。

2. 对齐dotnet版本与Visual Studio使用版本

  • 查看Visual Studio使用的dotnet版本:依次进入「工具」→「选项」→「项目和解决方案」→「.NET Core」,记录对应版本。
  • 检查命令行当前dotnet版本:
    dotnet --version
    
  • 若版本不一致,可通过以下方式切换:
    • 在项目根目录创建global.json文件指定版本:
      {
        "sdk": {
          "version": "x.x.x"
        }
      }
      
    • 使用dotnet版本选择器(如dotnet --list-sdks查看已安装版本,dotnet new globaljson --sdk-version x.x.x生成配置)。

3. 清除NuGet缓存后重试

执行命令清除本地NuGet缓存,避免缓存损坏导致的访问问题:

dotnet nuget locals all --clear

之后重新运行dotnet restore。

4. 在PowerShell脚本中直接嵌入凭据(批量构建场景)

为避免依赖本地配置,可在脚本中直接传入凭据参数:

# 定义变量
$azureDevOpsPat = "你的PAT令牌"
$nugetSource = "https://pkgs.dev.azure.com/[你的组织名]/[你的项目名]/nuget/v3/index.json"
$solutionPath = "C:\Users\...\MyProject.sln"

# 执行还原
dotnet restore $solutionPath --source $nugetSource --username any --password $azureDevOpsPat --store-password-in-clear-text

# 后续批量构建命令示例
$configurations = @("Debug", "Release")
foreach ($config in $configurations) {
    dotnet build $solutionPath --configuration $config --no-restore
}

原因说明

Visual Studio会自动复用当前登录Azure DevOps的会话凭据,而dotnet命令行工具默认不会关联这些凭据,需手动配置或传入参数完成身份验证。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 05:07:08