NuGet package source authentication: forcing credential refresh after API key expiration
0 reputation · 01 Jun 2020, 11:03 UTC
0 reputation · 01 Jun 2020, 11:03 UTC
I want NuGet restore operations (via nuget.exe and the .NET SDK) to stop using a cached API key once that key expires or its permissions are revoked, and instead re-request credentials at that point. The broader aim is least-privilege authentication: short-lived keys scoped per source, without stale credentials silently lingering in the cache.
My understanding is that NuGet caches credentials per configured package source and reuses them until the process restarts, surfacing expired or revoked keys only as 401/403 responses after a failed download. The SDK exposes an ICredentialsProvider extensibility point, but it is unclear to me whether NuGet actually re-invokes a custom provider after a cached credential fails, or whether the cache must be cleared manually. I am also unsure how NuGet 6.x changes to credential persistence affect existing custom provider implementations.
ICredentialsProvider when a cached credential returns 401/403, or is manual cache clearing the only built-in recovery path?34775 reputation · 01 Jun 2020, 11:13 UTC
NuGet does retry a request through a credential provider when a source returns a 401 challenge — but the credential the provider returns is cached for the lifetime of the process, keyed by source URI. A second 401 in the same restore process does not reliably cause NuGet to re-acquire credentials. So the practical answer to your goal is: put the expiry logic inside the credential provider, and treat process restart as the only guaranteed way to drop a session-cached credential. There is no built-in per-source TTL setting.
Your understanding is partly right and partly conflates two different mechanisms:
setapikey or <packageSourceCredentials>) are static. They are used as-is until you edit the file. Nothing refreshes them, and they may be stored in plaintext at user level — a poor fit for short-lived keys.One correction to your mental model: ICredentialsProvider is an internal NuGet SDK interface, not a public plugin contract. The supported extensibility point is the credential provider plugin model — a standalone executable discovered by NuGet (conventionally named credentialprovider.* on PATH) that communicates over stdio. Compiling against the internal interface will break across NuGet versions.
dotnet nuget locals http-cache --clear clears the on-disk HTTP cache, but session-cached credentials live in the process. A new dotnet restore invocation is the reliable reset.Run restore with detailed verbosity and have your provider log every invocation with a timestamp:
dotnet restore -v detailed
dotnet nuget locals all --listThen expire the key mid-session and restore twice. If the provider log shows only one invocation, the second request used the session-cached credential — confirming that manual/process-level clearing is the recovery path, not automatic re-acquisition. Grep the detailed output for 401/403 responses and provider messages to correlate.
Retry-and-recache semantics differ across NuGet 5.x/6.x and between nuget.exe, the dotnet CLI, and Visual Studio. Check your versions (dotnet --version) against the NuGet release notes for credential-provider changes before depending on exact behavior. The one diagnostic detail that would change this recommendation: which client (nuget.exe vs. dotnet CLI vs. Visual Studio) is failing, since their caching behavior is not identical.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.