How to Resolve Unauthorized Access Issue with Private NuGet Repository

How to Resolve Unauthorized Access Issue with Private NuGet Repository

How to Resolve Unauthorized Access Issue with Private NuGet Repository

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 pressing Command + 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!

Recent Posts

HybridCache in .NET 9 is Awesome!
.NET 9 is now live, and it comes with a new set of features. Some are great, and some are just icing on the cake. …
Adding Custom Formatting to Your Classes with IFormattable
DateTime has a great feature that I often replicate in my classes: the ability for users to format the ToString output however they want. Using …
Azure-Sync: Sync your Azure App Settings to local
Azure-Sync is a handy shell script tool designed to help .NET developers working with Azure App Services. Inspired by the functionality provided by the Azure …
Implement Builders easily with Source Generator in .NET
I created a YouTube video on Source Generator in which I showcased one possible implementation. However, I feel that I didn’t fully highlight its capabilities. …

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.