Does NuGet Recover From Expired Feed Credentials During Non-Interactive CI Restores?
0 reputation · 17 Mar 2022, 14:04 UTC
0 reputation · 17 Mar 2022, 14:04 UTC
Private feeds such as Azure Artifacts authenticate through static credentials in NuGet.config or through credential provider plugins such as the Azure Artifacts Credential Provider, which acquires short-lived Microsoft Entra ID tokens. Least-privilege guidance favors narrow scopes: read-only access for restore, package-scoped API keys for push.
The unresolved point is expiry. NuGet does not refresh static NuGet.config credentials, so an expired entry surfaces as 401 Unauthorized during restore and keeps failing until the file is edited. Credential providers can re-acquire tokens, but how reliably that happens in a non-interactive CI run is unclear, and NuGet's per-session credential cache may keep serving stale failures after the credential is renewed. Behavior also differs across dotnet CLI, NuGet.exe, and Visual Studio.
For an unattended restore-only pipeline, the trade-off is a long-lived narrowly scoped credential that will eventually expire versus a provider-based short-lived token whose silent renewal in headless environments still needs verification against current NuGet clients.
35275 reputation · 17 Mar 2022, 23:38 UTC
The Azure Artifacts Credential Provider cannot silently recover from an expired identity (such as an expired Service Principal secret or PAT) during a non-interactive restore; it will fail with a 401 Unauthorized error. However, it can silently refresh a short-lived access token if the underlying identity remains valid and the provider is correctly configured for headless environments.
It is critical to distinguish between the access token and the identity credential:
VSS_NUGET_EXTERNAL_FEED_ENDPOINTS environment variable or a valid Service Principal is present.NuGet.config.NuGet generally relies on the provider to handle the handshake. If a provider successfully refreshes a token, NuGet uses the updated credential for subsequent requests. However, if the provider fails and returns a 401, some versions of the dotnet CLI may cache the failure state for the duration of the process. In these cases, the build process must be restarted after the credential is fixed to clear the session state.
NuGet maps credentials to the source URL. You cannot bind two different identities to the same feed URL simultaneously within a single session. To use a read-only identity for restore and a write-identity for push on the same feed, you must either:
Project Contributor (push) and Reader (restore) permissions.To ensure your headless environment is configured for silent renewal, verify the following:
NUGET_PLUGIN_PATHS is set to the directory containing the Azure Artifacts Credential Provider.401 Unauthorized (expired/invalid credentials) versus 403 Forbidden (valid credentials, insufficient permissions).Diagnostic Request: To provide a more specific recovery path, please specify if you are using a Personal Access Token (PAT) or a Service Principal (App Registration) for your CI identity.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.