NuGet to work behind a proxy

Published by Jayaraj on

In this article, I am going to explain – the Configuration steps for NuGet to work behind a proxy.

What is NPM?

NuGet is the package manager for the Microsoft development platform including .NET.
When you use NuGet to install a package, it copies the library files to your solution and automatically updates your project (add references, change config files, etc.). If you remove a package, NuGet reverses whatever changes it made so that no clutter is left.

Problem:
If you work behind a proxy, when you try to add a NuGet package to your Visual Studio project, probably this message will compare in the output tab:

error: Failed to retrieve information from remote source …
error: Response status code does not indicate success: 407 (Proxy Authentication Required).

It means that NuGet is trying to download the package, but the proxy is blocking.

Solution:
Open the file C:\Users\[YOUR_USER_NAME]\AppData\Roaming\NuGet\NuGet.Config and add inside the <\configuration> tag the following:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
  </packageSources>
  <disabledPackageSources>
    <add key="Microsoft and .NET" value="true" />
  </disabledPackageSources>
  <config>
    <add key="http_proxy" value="[YOUR_PROXY_ADDRESS]:[YOUR_PROXY_PORT]" />
  </config>
</configuration>

The file location can also be found from the properties of the Nuget location from Visual Studio as in below figure:


Note: Replacing,
[YOUR_USER_NAME] with your Windows account name
[YOUR_PROXY_ADDRESS] with the address of the proxy
[YOUR_PROXY_PORT] with the port of the proxy

Open Visual Studio and NuGet should automatically restore the packages.


1 Comment

Andy · August 9, 2020 at 10:51 pm

Thank you so much. I accidentally cleared Nuget cache files and in consequence ‘dotnet restore’ command needed to make a request to nuget web site. I only can provide Internet to my laptop through a proxy and I didn’t know how to tell ‘dotnet restore’ use that proxy. Thanks for your help

Leave a Reply

Avatar placeholder

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.