Warm tip: This article is reproduced from stackoverflow.com, please click
.net nuget nuget-package-restore visual-studio-code private-nuget-feed

How do I restore Nuget Packages from a private NugetFeed in a devcontainer on Windows?

发布于 2020-04-04 10:14:09

I am using Windows, installed Docker (using lunix containers), installed VS Code and the devcontainer extension. I loaded a project which is using Nugets from a private NugetFeed and as i am trying to restore (interactive) the NugetPackages I am getting a 401 error from my NugetFeed.

/usr/share/dotnet/sdk/2.2.207/NuGet.targets(119,5): error : Unable to load the service index for source https://pkgs.dev.azure.com/[SomeFeedUrl]/nuget/v3/index.json. [/workspaces/[SomeSolution].sln]
/usr/share/dotnet/sdk/2.2.207/NuGet.targets(119,5): error :   Response status code does not indicate success: 401 (Unauthorized). [/workspaces/[SomeSolution].sln]

I tried the same thing locally (credential provider installed) and everything is working fine. I also tried mounting the Nugetpackages path to the devcontainer, restore locally and then start the project (since this is my fix to get the restore working properly on my linux maschine).

Here's what my devcontainer.json looks like:

// For format details, see https://aka.ms/vscode-remote/devcontainer.json or the definition README at
// https://github.com/microsoft/vscode-dev-containers/tree/master/containers/azure-functions-dotnetcore-2.2
{
    "name": "Azure Functions & C# (.NET Core 2.2)",
    "dockerFile": "Dockerfile",

    // Use 'settings' to set *default* container specific settings.json values on container create. 
    // You can edit these settings after create using File > Preferences > Settings > Remote.
    "settings": { 
        "terminal.integrated.shell.linux": "/bin/bash"
    },

    "mounts": [
        "source=C:/Users/[user]/.nuget,target=/home/vscode/.nuget,type=bind"
        // "source=${env:HOME}${env:USERPROFILE}/.aspnet/https,target=/home/vscode/.aspnet/https,type=bind"
    ],

    "remoteEnv": {
        // [Optional] Override the default HTTP endpoints - need to listen to '*' for appPort to work
        "ASPNETCORE_Kestrel__Endpoints__Http__Url": "http://*:5000"
        // "ASPNETCORE_Kestrel__Endpoints__Https__Url": "https://*:5001",
        // "ASPNETCORE_Kestrel__Certificates__Default__Password": "SecurePwdGoesHere",
        // "ASPNETCORE_Kestrel__Certificates__Default__Path": "/home/vscode/.aspnet/https/aspnetapp.pfx",
    },

    // Uncomment the next line to run commands after the container is created.
    // "postCreateCommand": "dotnet restore",

    // Uncomment the next line to have VS Code connect as an existing non-root user in the container. 
    // On Linux, by default, the container user's UID/GID will be updated to match your local user. See
    // https://aka.ms/vscode-remote/containers/non-root for details on adding a non-root user if none exist.
    // "remoteUser": "vscode",

    // Add the IDs of extensions you want installed when the container is created in the array below.
    "extensions": [
        "ms-azuretools.vscode-azurefunctions",
        "ms-vscode.csharp"
    ]
}

I did not touch the dockerfile, the dockerfile is still a stock dockerfile.

Does anyone have the same issue/know how to get it working properly?

Questioner
thegreatdane6
Viewed
379
TheAifam5 2020-01-31 23:17

Currently, there is no clean way to do it. I think the #1 solution should be much easier to setup.

Solution #1

  1. Open .devcontainer/devcontainer.json and add entry to mounts section (you can change the src value to something else - that's the name of the volume where your token will be stored):

    "src=credprovider-data,dst=${env:HOME}/.local/share/MicrosoftCredentialProvider,type=volume"

  2. Open .devcontainer/Dockerfile add at the end:
# Download and install Azure DevOps Credential provider
&& curl -sL https://raw.githubusercontent.com/microsoft/artifacts-credprovider/master/helpers/installcredprovider.sh | bash -
  1. Save both files and rebuild container
  2. Open bash in the container and execute: dotnet restore --interactive
  3. Click on the link, that should open your browser. Type the code and press ENTER. Now, depends if you are logged in, will ask you to log in. After logging in to your Azure DevOps, you will see message that you can now close the window. Some seconds later you will see in your DevContainer, that dotnet restore continues and downloads your feed.

This method should be persistent after restarts and even the container gets rebuild.

Solution #2

You can also pass PAT (Personal Access Token) as VSS_NUGET_ACCESSTOKEN environment, Feed URL, set the VSS_NUGET_EXTERNAL_FEED_ENDPOINTS and the NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED environment variables in the container.

Please check also here: https://github.com/Microsoft/artifacts-credprovider#help

Github Issues related to your question: