Why does authentication not working with Nuget in Nexus 3?

In a newly created Sonatype Nexus (version 3) server I’m trying to use a Hosted Nuget server.

I used nuget setapikey to save the API key of my admin user, and now my nuget.config file looks like this (I omitted the encrypted key):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <packageSources>
  <add key="nuget" value="https://api.nuget.org/v3/index.json" />
  <add key="sonatype2" value="https://nexus3-dev.my-company.tech/repository/nuget-hosted/" />
 </packageSources>
  <apikeys>
    <add key="https://nexus3-dev.my-company.tech/repository/nuget-hosted/" value="abc123..." />
  </apikeys>
</configuration>

Yet when I try to do just a query for packages by adding a reference, I get 401 Unauthorized errors

$ dotnet add package System.Net.Http
  Writing C:\Users\mvincze\AppData\Local\Temp\tmpB99C.tmp
info : Adding PackageReference for package 'System.Net.Http' into project 'C:\Workspaces\Local\NexusTest\NexusTest.csproj'.
log  : Restoring packages for C:\Workspaces\Local\NexusTest\NexusTest.csproj...
info :   GET https://nexus3-dev.my-company.tech/repository/nuget-hosted/FindPackagesById()?id='System.Net.Http'&semVerLevel=2.0.0
info :   GET https://dotnetfeed.blob.core.windows.net/dotnet-core/flatcontainer/system.net.http/index.json
info :   GET https://api.nuget.org/v3-flatcontainer/system.net.http/index.json
info :   Unauthorized https://nexus3-dev.my-company.tech/repository/nuget-hosted/FindPackagesById()?id='System.Net.Http'&semVerLevel=2.0.0 641ms
log  : Retrying 'FindPackagesByIdAsyncCore' for source 'https://nexus3-dev.my-company.tech/repository/nuget-hosted/FindPackagesById()?id='System.Net.Http'&semVerLevel=2.0.0'.
log  : Response status code does not indicate success: 401 (Unauthorized).
info :   GET https://nexus3-dev.my-company.tech/repository/nuget-hosted/FindPackagesById()?id='System.Net.Http'&semVerLevel=2.0.0
info :   Unauthorized https://nexus3-dev.my-company.tech/repository/nuget-hosted/FindPackagesById()?id='System.Net.Http'&semVerLevel=2.0.0 228ms
log  : Retrying 'FindPackagesByIdAsyncCore' for source 'https://nexus3-dev.my-company.tech/repository/nuget-hosted/FindPackagesById()?id='System.Net.Http'&semVerLevel=2.0.0'.
log  : Response status code does not indicate success: 401 (Unauthorized).
info :   GET https://nexus3-dev.my-company.tech/repository/nuget-hosted/FindPackagesById()?id='System.Net.Http'&semVerLevel=2.0.0
info :   OK https://api.nuget.org/v3-flatcontainer/system.net.http/index.json 500ms
info :   Unauthorized https://nexus3-dev.my-company.tech/repository/nuget-hosted/FindPackagesById()?id='System.Net.Http'&semVerLevel=2.0.0 216ms
error: Failed to retrieve information about 'System.Net.Http' from remote source 'https://nexus3-dev.my-company.tech/repository/nuget-hosted/FindPackagesById()?id='System.Net.Http'&semVerLevel=2.0.0'.
error:   Response status code does not indicate success: 401 (Unauthorized).

What can be the reason for this, or how can I troubleshoot what setting I should adjust to get the API key working?

A guess… has the “NuGet API-Key Realm” been activated under “security/realms” in the admin UI?

Hi @rseddon,

Yep, I found that somewhere as a suggestion, so I tried it, but it didn’t help.
This is what the Realms look like:

I should have looked at your log closer… the failure is on a GET, not a publish. NuGet uses API keys for publishing, not retrieval. You’ll need to store your password as outlined here:

Hi @rseddon,

Thanks a lot, this was the issue, now pulling packages works properly. :+1:

Cheers,
Mark