Have you ever encountered an issue while trying to restore packages from a private NuGet repository in JetBrains Rider using Azure DevOps? I recently faced a similar problem and struggled with the “401 Unauthorized” error. In this blog post, I will guide you through the steps I took to fix this issue. So, let’s dive in!
Azure DevOps and Private NuGet Repositories
Azure DevOps is a comprehensive platform that provides a range of tools for software development, including package management with NuGet. Private NuGet repositories in Azure DevOps allow you to host and manage your own packages. You may encounter authentication challenges when you try to restore packages from a private NuGet repository.
Modify NuGet.config Global File
To resolve the unauthorized access issue, we need to modify the NuGet.config
file, which is a global configuration file JetBrains Rider uses to manage NuGet package sources. Follow these steps:
1. Locate the NuGet.config file
- Windows: The NuGet.config file is typically located at the following path:
%APPDATA%\NuGet\NuGet.Config
. You can easily access it by pasting this path into the File Explorer’s address bar. - Mac: The NuGet.config file is usually located at the following path:
~/.nuget/NuGet/NuGet.Config
. You can navigate to this path using Finder by pressingCommand + Shift + G
and entering the path.
If the file doesn’t exist, create a new one. Open the NuGet.config
file in any text editor
2. Add package source and credentials
Inside the <packageSources>
section, add a new <add>
element to specify the URL of your private NuGet repository. Replace YOUR_REPOSITORY_URL
with the actual URL of your repository.
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="YourPrivateRepo" value="YOUR_REPOSITORY_URL" />
</packageSources>
3. Provide authentication credentials
Under the <packageSourceCredentials>
section, add a new <YourPrivateRepo>
element to store the authentication credentials for your private repository. Replace YOUR_USERNAME
and YOUR_PAT
with your actual Azure DevOps username and Personal Access Token.
<packageSourceCredentials>
<YourPrivateRepo>
<add key="Username" value="-- needed but not used --" />
<add key="ClearTextPassword" value="%PAT%" />
</YourPrivateRepo>
</packageSourceCredentials>
4. Creating a Personal Access Token (PAT) in Azure DevOps
You can use a personal access token (PAT) as an alternate password to authenticate into Azure DevOps, check the official Microsoft documentation to know how to create PAT to be used in the NuGet.config
5. Store the PAT in Environment Variables
Since the password key is “ClearTextPassword”, it’s a terrible idea and a security concern if you’re saving NuGet.config
with a clear PAT. So it’s best to store the PAT in an environment variable.
Also, when your PAT expires, you can simply get a new one and update the PAT variable value there.
Make sure to save the environment variable with the same name used in the NuGet.config
file, in our example we used PAT
6. Save and close
After making the necessary modifications, save the NuGet.config
file and close the text editor. Make sure also to close and reopen JetBrains Rider to refresh the environment variables.
7. Success!
Launch JetBrains Rider, open your .NET solution and right-click on the solution node in the Solution Explorer. Choose “Manage NuGet Packages” from the context menu and then click on Restore. JetBrains Rider will handle the package restoration process using the updated NuGet.config
file. Sit back and let the packages from your private NuGet repository be successfully restored.
8. In case of failure, try this
Another solution is to install the Azure Artifacts Credential Provider provided by Microsoft. You can find it here: https://github.com/Microsoft/artifacts-credprovider
On Windows
iex "& { $(irm https://aka.ms/install-artifacts-credprovider.ps1) }"
On Mac or Linux
wget -qO- https://aka.ms/install-artifacts-credprovider.sh | bash
After installation, you’ll be able to perform an interactive ‘dotnet restore’ that prompts you to log in using your Microsoft credentials:
dotnet restore --interactive
Conclusion
By following these simple steps, you can overcome the “401 Unauthorized” issue when restoring packages from a private NuGet repository in JetBrains Rider using Azure DevOps. Modifying the NuGet.config file with the appropriate package source and authentication credentials, creating a Personal Access Token (PAT) in Azure DevOps, and securely storing the PAT in environment variables ensure a smooth and secure package restoration process. Another option is to use the Artifacts Credential Provider provided by Microsoft to perform an interactive dotnet restore
using your Microsoft login.
Now you can enjoy hassle-free package management and continue developing your .NET solution with ease. Happy coding!