Skip to content

Commit 7c43b0b

Browse files
authored
Rewrite docs for authenticated feeds (#3201)
1 parent 9c6c26e commit 7c43b0b

File tree

2 files changed

+80
-67
lines changed

2 files changed

+80
-67
lines changed

docs/consume-packages/consuming-packages-authenticated-feeds.md

Lines changed: 78 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,98 +3,111 @@ title: Consuming packages from authenticated feeds
33
description: Consuming packages from authenticated feeds in all NuGet client scenarios
44
author: nkolev92
55
ms.author: nikolev
6-
ms.date: 02/28/2020
6+
ms.date: 12/22/2023
77
ms.topic: conceptual
88
---
99

1010
# Consuming packages from authenticated feeds
1111

12-
In addition to the nuget.org [public feed](https://api.nuget.org/v3/index.json), NuGet clients have the ability to interact with file feeds and private http feeds.
12+
Many NuGet operations, such as restore and install, require communication with one or more package sources, which [can be configured in *nuget.config* files](../reference/nuget-config-file.md#packagesources).
13+
For HTTP feeds, NuGet will make an unauthenticated request, and if the server responds with an HTTP 401 response, NuGet will search for credentials in the following order:
1314

15+
1. [An environment variable `NuGetPackageSourceCredentials_{name}`](#credentials-in-environment-variables).
16+
1. [Credentials in *nuget.config* files](#credentials-in-nugetconfig-files).
17+
1. [Use a NuGet credential provider, if your package source provides one](#credential-providers).
1418

15-
To authenticate with private http feeds, the 2 approaches are:
19+
> [!NOTE]
20+
> We recommend using a credential provider when possible.
21+
> Using a credential provider avoids secrets in the *nuget.config* file, reducing risk of accidentally leaking secrets via source control.
22+
> Additionally, it typically reduces the number of places you need to update when a credential expires or changes.
23+
> If the credential provider supports single sign-on, it may reduce the number of times you need to login, or the number of places that credentials need to be saved.
1624
17-
* Add credentials in the [NuGet.config](../reference/nuget-config-file.md#packagesourcecredentials)
18-
* Authenticate using one of the many extensibility models depending on the client used.
25+
The credentials you need to use are determined by the package source.
26+
Therefore, unless you're using a credential provider, you should check with your package source for what credentials to use.
27+
It is very common for package sources to forbid you from using your password (that you log into the website with) with NuGet.
28+
Typically you need to create a Personal Access Token to use as NuGet's password, but you should check the documentation for the NuGet server you're using.
29+
Some package sources, such as Azure DevOps and GitHub, have scoped access tokens, so you may need to ensure that any tokens you create include the required scope.
1930

20-
## NuGet clients' authentication extensibility
31+
## Credentials in environment variables
2132

22-
For the various NuGet clients, the private feed provider itself is responsible for authentication.
23-
All NuGet clients have extensibility methods to support this. These are either a Visual Studio extension or a plugin that can communicate with NuGet to retrieve credentials.
33+
NuGet will search for an environment variable named `NuGetPackageSourceCredentials_{name}`, where `{name}` is the value of `key="name"` in your *nuget.config* file's package source.
34+
The value of the environment variable must be `Username={username};Password={password}`, and may optionally include `;ValidAuthenticationTypes={types}`.
35+
If the environment variable doesn't match NuGet's convention, or the value doesn't meet NuGet's expected pattern, NuGet will silently ignore the environment variable, and continue searching for credentials for the package source elsewhere.
36+
There are no logs to signal that NuGet uses the credential from the environment variable, which can cause difficulties in debugging authentication problems if the environment variable contains an expired secret, and the new secret is added to a *nuget.config* file, since the config file has lower precedence.
2437

25-
### Visual Studio
38+
> [!TIP]
39+
> Using environment variables in CI/CD pipelines is an excellent choice to minimize the risk of secrets being captured in logs.
2640
27-
In Visual Studio, NuGet exposes an interface that feed providers can implement and provide to their customers. For more details, please refer to the documentation on [how to create a Visual Studio credential provider](../reference/extensibility/NuGet-Credential-Providers-for-Visual-Studio.md).
41+
For example, consider the following *nuget.config* file:
2842

29-
#### Available NuGet credential providers for Visual Studio
43+
```xml
44+
<configuration>
45+
<packageSources>
46+
<clear />
47+
<add key="Contoso" value="https://nuget.contoso.com/v3/index.json" />
48+
</packageSources>
49+
</configuration>
50+
```
3051

31-
There is a credential provider built into Visual Studio to support Azure DevOps.
52+
In this case, the source name is `Contoso` and NuGet will look for the environment variable name `NuGetPackageSourceCredentials_Contoso`.
53+
Some platforms are case-sensitive, so take care about using the correct upper and lower case characters for the environment name and the source name, as defined in your *nuget.config* file.
3254

55+
If the username is `nugetUser` and the password is `secret123`, the environment variable's value should be set to `Username=nugetUser;Password=secret123`.
56+
If NuGet should only use this credential for HTTP Basic authentication, but not other authentication schemes, you can set the environment variable's value to `Username=nugetUser;Password=secret123;ValidAuthenticationTypes=Basic`.
57+
For more information about valid authentication types, see [the docs on package credentials in *nuget.config* files](../reference/nuget-config-file.md#packagesourcecredentials).
3358

34-
Available plug-in credential providers include:
59+
> [!NOTE]
60+
> Environment variables have restrictions on allowed characters, and different operating systems may have different restrictions.
61+
> For example, spaces are not allowed.
62+
> Therefore, you use this environment variable feature to specify NuGet credentials for package sources that use any characters that are invalid for your platform's environment variables.
63+
> In such cases, you should rename the package source in your *nuget.config* file.
3564
36-
* [MyGet Credential Provider for Visual Studio](http://docs.myget.org/docs/reference/credential-provider-for-visual-studio)
65+
## Credentials in *nuget.config* files
3766

38-
### nuget.exe
67+
*nuget.config* files can contain package source credentials.
68+
See [the *nuget.config* file reference doc section on package source credentials](../reference/nuget-config-file.md#packagesourcecredentials) for more information, including syntax.
69+
However, it's easier to use [`dotnet nuget update source`](/dotnet/core/tools/dotnet-nuget-update-source) on the command line to set the credentials.
3970

40-
When `nuget.exe` needs credentials to authenticate with a feed, it looks for them in the following manner:
71+
> ![Warning]
72+
> Take care when setting credentials in *nuget.config* files, especially when saving the credential as plain text.
73+
> If the credential is written to a *nuget.config* file that is in source control, there is an increased risk of accidentally leaking the secret.
74+
>
75+
> As [NuGet accumulates settings from multiple files](../consume-packages/configuring-nuget-behavior.md), it is recommended to save credentials to your user *nuget.config* file.
76+
> We also recommend to save package sources in the solution (source code repository) *nuget.config* file, including a `<clear />` element, for build reliability.
4177
42-
1. Look for credentials in `NuGet.config` files.
43-
1. Use V2 plug-in credential providers
44-
1. Use V1 plug-in credential providers
45-
1. NuGet then prompts the user for credentials on the command line.
78+
The username and plain text password in a *nuget.config* file can use an environment variable by adding `%` to the beginning and end of the environment variable name you would like to use.
79+
For more information, see [the *nuget.config* reference docs on using environment variables](../reference/nuget-config-file.md#using-environment-variables).
4680

47-
#### nuget.exe and V2 credential providers
81+
## Credential providers
4882

49-
In version `4.8` NuGet defined a new authentication plugin mechanism, hereafter referred to as V2 credential providers.
50-
For the installation and discovery of those providers, refer to [NuGet cross platform plugins](../reference/extensibility/NuGet-Cross-Platform-Plugins.md#plugin-installation-and-discovery).
83+
NuGet has an extensibility model, allowing [plugins to provide NuGet credentials](../reference/extensibility/NuGet-Cross-Platform-Authentication-Plugin.md).
84+
The [path that credential providers must be installed](../reference/extensibility/NuGet-Cross-Platform-Plugins.md#plugin-installation-and-discovery), for NuGet to discover, is different for .NET Framework (NuGet.exe, MSBuild, and Visual Studio), and the .NET SDK (running on the .NET 5+ runtime).
5185

52-
#### nuget.exe and V1 credential providers
86+
NuGet has a concept of being run in interactive mode or non-interactive mode.
87+
When in non-interactive mode, credential providers are asked not to block NuGet.
88+
While in interactive mode, the credential provider may prompt you to log in.
89+
Different tools have different defaults, so interactive mode may need to be opt-in or opt-out, depending on your scenario.
5390

54-
In version `3.3` NuGet introduced the first version of authentication plugins.
55-
For the installation and discovery of those providers refer to [nuget.exe credential providers](../reference/extensibility/nuget-exe-Credential-Providers.md#nugetexe-credential-provider-discovery)
91+
|Tool|Default|Toggle|
92+
|--|--|--|
93+
|`dotnet` CLI|non-interactive|`--interactive` argument. For example, `dotnet restore --interactive`.|
94+
|MSBuild|non-interactive|`NuGetInteractive` MSBuild property. For example, `msbuild -t:restore -p:NuGetInteractive=true`.|
95+
|NuGet.exe|interactive|`-NonInteractive` argument. For example, `nuget.exe restore -NonInteractive`.|
96+
|Visual Studio|interactive|not possible to run in non-interactive mode.|
5697

57-
#### Available credential providers for nuget.exe
98+
[NuGet.exe supports both V1 and V2 credential providers](../reference/extensibility/nuget-exe-Credential-Providers.md), while MSBuild and the .NET SDK only support the cross platform (V2) plugins.
5899

59-
* [Azure DevOps V2 Credential Providers](/azure/devops/artifacts/nuget/nuget-exe#add-a-feed-to-nuget-482-or-later) or [Azure Artifacts Credential Provider](https://github.com/microsoft/artifacts-credprovider)
100+
In Visual Studio, NuGet has a [Visual Studio Credential Provider interface](../reference/extensibility/NuGet-Credential-Providers-for-Visual-Studio.md), which credential providers can use to provide a graphical login experience, or call Visual Studio APIs if necessary.
101+
NuGet in Visual Studio will fall back to the command line credential providers if it can't find a Visual Studio credential provider that handles the source.
60102

61-
With Visual Studio 2017 version 15.9 and later, the Azure DevOps credential provider is bundled in Visual Studio.
62-
If `nuget.exe` uses MSBuild from that specific Visual Studio toolset, then the plugin will be discovered automatically.
103+
Visual Studio 2017 version 15.9, and above, includes a credential provider for [Azure Artifacts](/azure/devops/artifacts/), that works within Visual Studio, MSBuild, and NuGet.exe.
104+
However, the credential provider for the .NET SDK is not included by Visual Studio, so [must be installed separately](https://github.com/microsoft/artifacts-credprovider?tab=readme-ov-file#setup) to work with the `dotnet` CLI.
63105

64-
### dotnet.exe
106+
### List of credential providers
65107

66-
When `dotnet.exe` needs credentials to authenticate with a feed, it looks for them in the following manner:
108+
There is a [feature request to make credential providers installable via .NET tools](https://github.com/NuGet/Home/issues/12567), and this will likely make it easier to discover other credential providers.
109+
Until this is implemented, here is a list of credential providers we are aware of:
67110

68-
1. Look for credentials in `NuGet.config` files.
69-
1. Use V2 plug-in credential providers
70-
71-
By default `dotnet.exe` is not interactive, so you might need to pass an `--interactive` flag to get the tool to block for authentication.
72-
73-
#### dotnet.exe and V2 credential providers
74-
75-
In version `2.2.100` of the SDK, NuGet defined an authentication plugin mechanism that works in all clients.
76-
For the installation and discovery of those providers, refer to [NuGet cross platform plugins](../reference/extensibility/NuGet-Cross-Platform-Plugins.md#plugin-installation-and-discovery).
77-
78-
#### Available credential providers for dotnet.exe
79-
80-
* [Azure Artifacts Credential Provider](https://github.com/microsoft/artifacts-credprovider)
81-
82-
### MSBuild.exe
83-
84-
When `MSBuild.exe` needs credentials to authenticate with a feed, it looks for them in the following manner:
85-
86-
1. Look for credentials in `NuGet.config` files
87-
1. Use V2 plug-in credential providers
88-
89-
By default `MSBuild.exe` is not interactive, so you might need to set the `/p:NuGetInteractive=true` property to get the tool to block for authentication.
90-
91-
#### MSBuild.exe and V2 credential providers
92-
93-
In Visual Studio 2019 Update 9, NuGet defined an authentication plugin mechanism that works in all clients.
94-
For the installation and discovery of those providers, refer to [NuGet cross platform plugins](../reference/extensibility/NuGet-Cross-Platform-Plugins.md#plugin-installation-and-discovery).
95-
96-
#### Available credential providers for MSBuild.exe
97-
98-
* [Azure Artifacts Credential Provider](https://github.com/microsoft/artifacts-credprovider)
99-
100-
With Visual Studio 2017 Update 9 and later, the Azure DevOps credential provider is bundled in Visual Studio. No additional steps are required.
111+
* [AWS CodeArtifact NuGet Credential Provider](https://docs.aws.amazon.com/codeartifact/latest/ug/nuget-cli.html#nuget-configure-cli)
112+
* [Azure Artifacts Credential Provider](https://github.com/microsoft/artifacts-credprovider). This link is just for the command line credential provider.
113+
* [MyGet Credential Provider for Visual Studio](http://docs.myget.org/docs/reference/credential-provider-for-visual-studio).

docs/reference/extensibility/nuget-exe-Credential-Providers.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ ms.topic: conceptual
99

1010
# Authenticating feeds with nuget.exe credential providers
1111

12-
In version `3.3` support was added for `nuget.exe` specific credential providers. Since then, in version `4.8` [support for credential providers](NuGet-Cross-Platform-Authentication-Plugin.md) that work across all command line scenarios (`nuget.exe`, `dotnet.exe`, `msbuild.exe`) was added.
12+
In version `3.3` support was added for `nuget.exe` specific (v1) credential providers. Since then, in version `4.8` [support for (v2) credential providers](NuGet-Cross-Platform-Authentication-Plugin.md) that work across all command line scenarios (`nuget.exe`, `dotnet.exe`, `msbuild.exe`) was added.
1313

14-
See [Consuming Packages from authenticated feeds](../../consume-packages/consuming-packages-authenticated-feeds.md#nugetexe) for more details on all authentication approaches for `nuget.exe`
14+
See [Consuming Packages from authenticated feeds](../../consume-packages/consuming-packages-authenticated-feeds.md) for more details on all authentication approaches.
1515

1616
## nuget.exe credential provider discovery
1717

0 commit comments

Comments
 (0)