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

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

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.

 
 

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.

 
 

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

 
 

To my satisfaction, the error was finally gone.

Support me

I appreciate it if you would support me if have you enjoyed this post and found it useful, thank you in advance.

 

https://github.com/Microsoft/artifacts-credprovider#setup

Setup

If you are using dotnet or nuget, you can use the Azure Artifact Credential Provider by adding it to NuGet's plugin search path. This section contains both manual and scripted instructions for doing so.

Dotnet needs the netcore version to be installed. NuGet and MSBuild need the netfx version to be installed.

Installation on Windows

Automatic PowerShell script

PowerShell helper script

  • To install netcore, run installcredprovider.ps1
    • e.g. iex "& { $(irm https://aka.ms/install-artifacts-credprovider.ps1) }"
  • To install both netfx and netcore, run installcredprovider.ps1 -AddNetfx. The netfx version is needed for nuget.exe.
    • e.g. iex "& { $(irm https://aka.ms/install-artifacts-credprovider.ps1) } -AddNetfx"

Manual installation on Windows

  1. Download the latest release of Microsoft.NuGet.CredentialProvider.zip
  2. Unzip the file
  3. Copy the netcore (and netfx for nuget.exe) directory from the extracted archive to $env:UserProfile\.nuget\plugins (%UserProfile%/.nuget/plugins/)

Using the above is recommended, but as per NuGet's plugin discovery rules, alternatively you can install the credential provider to a location you prefer, and then set the environment variable NUGET_PLUGIN_PATHS to the .exe of the credential provider found in plugins\netfx\CredentialProvider.Microsoft\CredentialProvider.Microsoft.exe. For example, $env:NUGET_PLUGIN_PATHS="my-alternative-location\CredentialProvider.Microsoft.exe". Note that if you are using both nuget and dotnet, this environment variable is not recommended due to this issue: NuGet/Home#8151

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章