Using the .NET CLI to restore packages from a private NuGet feed

Tim Deschryver
Tim Deschryver
timdeschryver.dev
Reading mode

For the past years, I've been working in Visual Studio (the 2022 preview version is available), but recently I've given Visual Studio Code a shot. Transitioning between the two IDE's also means that you use the .NET CLI more extensively. Sadly, this was the cause of one annoying problem for me.

A couple of times a day, when I was trying to build a project, I bumped against the following error saying that I'm not authorized to access our private feed.

dotnet build
Determining projects to restore...
[C:\Users\tdeschryver\dev\Project\Project\02.Services\ServiceOne\ServiceOne.Api.csproj]
C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\NuGet.targets(131,5): error : Unable to load the service index for source https://pkgs.dev.azure.com/org/project/_packaging/feed/nuget/v3/index.json. [C:\Users\tdeschryver\dev\Project\Project.sln]
C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\NuGet.targets(131,5): error : Response status code does not indicate success: 401 (Unauthorized). [C:\Users\tdeschryver\dev\Project\Project.sln]

This led to frustrations and was counter-productive. As a work-around, I opened the project in Visual Studio to be authorized.

Some GitHub issues mentioned running the restore command with the interactive flag, but in my case, this ended up in the same error.

dotnet restore --interactive

After browsing through more GitHub issues, I found a comment that suggested installing the Microsoft NuGet CredentialProvider.

# On Windows
iex "& { $(irm https://aka.ms/install-artifacts-credprovider.ps1) }"
# On Linux
wget -qO- https://aka.ms/install-artifacts-credprovider.sh | bash

To my satisfaction, the error was finally gone.

Feel free to update this blog post on GitHub, thanks in advance!

Enjoying the blog?

Support my work

If you enjoyed this post and found it useful, consider supporting my work. It helps me keep creating and sharing content like this. Thank you!