为包添加 PackageReference 失败并显示 401(未经授权)

时间:2021-03-27 00:24:09

标签: authentication .net-core azure-devops nuget dotnet-cli

我正在尝试将第三方 NuGet 包(例如 StyleCop)安装到项目中。该项目还有从我们自己的私有 NuGet 源安装的包。在尝试安装该软件包时,我们收到了这样的 401 错误。

MyProject> dotnet add .\MyProject.csproj package StyleCop
  Determining projects to restore...
  Writing C:\Users\me\AppData\Local\Temp\tmp6A51.tmp
info : Adding PackageReference for package 'StyleCop' into project '.\MyProject.csproj'.
info :   CACHE https://api.nuget.org/v3/registration5-gz-semver2/stylecop/index.json
info :   GET https://mediavaletsc.pkgs.visualstudio.com/_packaging/c559332f-5d8d-4d3f-8fc2-6e06125775fa/nuget/v3/registrations2-semver2/stylecop/index.json
info :   Unauthorized https://mycompany.pkgs.visualstudio.com/_packaging/xxx-xxx-xxx-xxx-xxx/nuget/v3/registrations2-semver2/stylecop/index.json 680ms
error: Response status code does not indicate success: 401 (Unauthorized).

奇怪的是,即使在执行 dotnet restore 之后,运行 git clean -xfd 也能正常工作。我们如何配置 dotnet 和/或 nuget 以接受 StyleCop 或任何其他公共软件包的安装?

1 个答案:

答案 0 :(得分:0)

我们需要使用 Visual Studio Team Services (VSTS) 重新进行身份验证,因为 API 密钥已过期和/或被撤销。

dotnet restore 仍然有效,但 dotnet add ... package ... 失败的原因是前者使用了本地缓存,而后者命中了服务器。运行 nuget locals all -clear 后,恢复也停止工作。

要重新进行身份验证,请执行以下操作。

nuget sources remove -name $sourceName | Out-Null;
nuget sources add -name $sourceName -source $sourcePath -username $username -password $apiKey
相关问题