21

I try to learn Github action and packages. So, I create a sample nuget package and successfully created. However I could not use it.

I follow this step;

  1. Options > Nuget Package Manager > Package Sources
  2. Click add button write organization name and add package source address like that: enter image description here

  3. After that Visual Studio doesn't ask me any credential for this address.

  4. If there is no credential, I will expect to an error. And I got it.

    [github] Failed to retrieve metadata from source 'https://nuget.pkg.github.com/[OrganizationName]/query?q=&skip=0&take=26&prerelease=false&semVerLevel=2.0.0'. Response status code does not indicate success: 401 (Unauthorized).

  5. Then, I decide to some credential like explained in github documentation.

  6. My nuget.config file: enter image description here

  7. However still I get Unauthorized error. And also when I click the error link gettin a message like that:

{"errors":[{"code":"Your token has not been granted the required scopes to execute this query. The 'id' field requires one of the following scopes","message":" ['read:packages'], but your token has only been granted the: [''] scopes. Please modify your token's scopes at: https://github.com/settings/tokens."}]}

but I already have a token with read/write package credential. enter image description here

  1. All that fails and I try to another way. I add credential information to Windows Credential Manager. Eveything still same.

So, how I add and use my private nuget packages?

is_oz
  • 813
  • 1
  • 8
  • 27
  • 2
    If you use encrypted passwords,, you should add `` .Hint from [this document](https://learn.microsoft.com/en-us/nuget/reference/nuget-config-file#packagesourcecredentials). Besdies, please put `nuget.config` file under solution folder which exists `xxx.sln` file. Then close VS Instance, then restart your project again. It needs to restart VS to enable the new `nuget.config` file. – Mr Qian Jun 03 '20 at 06:54
  • And if does not work, please added all Repo scopes and all User scopes for your token. And then restart your project. – Mr Qian Jun 03 '20 at 06:56
  • 1
    @PerryQian-MSFT Thanks for restart idea. I restart computer and problem solved :) Probably why is all visual studio instances restarted. I already have a github issue in Visual Studio (https://github.com/github/VisualStudio/issues/2521) so I am not sure which issue rease what. – is_oz Jun 03 '20 at 07:05

3 Answers3

11

How to use github nuget packages on Visual Studio 2019

First, make sure that your credential info is correct on the nuget.config file.

I found your document suggest you should put a new nuget.config file to your solution. This nuget.config file is a local action file, it will act on any projects in the current subdirectory and below the current level. You can refer to this document.

In this way, you should restart VS Instance to enable this new nuget.config file.

-------Global nuget.config--------

However, from your description and first picture, you used the global nuget.config file. And UI's info stores under global nuget.config file.

If you want to config this gihub package source for all the projects on your PC, you should config it on the global nuget.config file.

And the first picture which you provided indicates that you used in the global file(C:\Users\xxx(current user)\AppData\Roaming\NuGet\NuGet.Config).

This function also needs restart VS to enable the new nuget.config file.

Suggestion

  1. please add your content of the file into C:\Users\xxx(current user)\AppData\Roaming\NuGet\NuGet.Config.

  2. restart VS Instance or restart PC to enable this new nuget.config file. It is designed by that.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Mr Qian
  • 21,064
  • 1
  • 31
  • 41
10

It is not an easy task, but here is the solution (after 2 days of struggling)

  1. First generate PAT (Personal Access Token) in Github
    Follow these steps to generate a PAT
    Very important to select the read:packages options enter image description here
    Github will show you the PAT for ONLY ONE TIME, So make sure to copy it to save place, otherwise, you have to generate it again

  2. Now with PAT in your hand, Add Nuget.Config file to your project
    enter image description here
    The content of the file should be like following

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <packageSources>
            <clear />
            <add key="github" value="https://nuget.pkg.github.com/OWNER/index.json" />
        </packageSources>
        <packageSourceCredentials>
            <github>
                <add key="Username" value="USERNAME" />
                <add key="ClearTextPassword" value="TOKEN" />
            </github>
        </packageSourceCredentials>
    </configuration>
    
  3. You must replace:

    • USERNAME with the name of your user account on GitHub.
    • TOKEN with your personal access token (the token you generated in Step 1).
    • OWNER with the name of the user or organization account that owns the repository containing your project.
  4. You must restart Visual Studio or even Restart the PC This is important

After that open the Terminal and copy and paste (maybe with some modifications) the statement that Github give to you to install the package

enter image description here

Now you are ready to get the Package.


UPDATE

How to configure github nuget packages for teams?

Configure the nuget.config using environment variables:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="NuGet official package source" value="https://api.nuget.org/v3/index.json" />
    <add key="Github" value="https://nuget.pkg.github.com/OWNER/index.json" />
  </packageSources>
  <packageSourceCredentials>
    <Github>
      <add key="Username" value="%GITHUB_PACKAGE_USER_NAME%" />
      <add key="ClearTextPassword" value="%GITHUB_PACKAGE_TOKEN%" />
    </Github>
  </packageSourceCredentials>
</configuration>

The GITHUB_PACKAGE_USER_NAME and GITHUB_PACKAGE_TOKEN can be anything you want.

Now, each team member should configure their user environment variables:

  • GITHUB_PACKAGE_USER_NAME: team member github user name
  • GITHUB_PACKAGE_TOKEN: team member Personal Access Token (PAT)

With those configurations, Visual Studio will be able to query and download packages, assuming the team member has access to the OWNER package repository.

How to restore github nuget packages in github actions workflows?

With the previous NuGet.config configured, you need to change your workflow passing the required environment variables, like so:

- name: Restore dependencies
  env:
    GITHUB_PACKAGE_USER_NAME: ${{ github.actor }}
    GITHUB_PACKAGE_TOKEN: ${{ secrets.RESTORE_ORGANIZATION_PACKAGES }}
  run: dotnet restore ./src

Since you can't pass your PAT, you need to configure a github secret (either for the repository or the organization). In the above example, I created a secret named RESTORE_ORGANIZATION_PACKAGES with read:packages permission at the repository level.

JobaDiniz
  • 862
  • 1
  • 14
  • 32
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
  • what if the repository is **shared** with the team? how can each team member configure its own PAT and add to the `nuget.config`? it seems wrong to have credentials in the `nuget.config`, which is shared and committed to the repository – JobaDiniz Feb 18 '22 at 20:58
  • 1
    @JobaDiniz yes, I agree with you, it did not make sense to me either, I think maybe the best solution, in this case, could be not adding the `NuGet.config` file to the repository, and keeping it local for each machine, not the best of solution, I know, but I could not think of something better – Hakan Fıstık Feb 19 '22 at 07:38
  • 1
    figured out a solution for teams. You can specify *environment variables* in the `nuget.config`. So, each team member needs to have 2 env vars (`%github_user_name%` and `%github_pat%`) configured in their Windows/Linux/Mac. – JobaDiniz Feb 19 '22 at 10:45
10

I just faced this problem today with VS 19.

When adding a new source for Nuget packages as shown below, i got prompted a first time for User and Password

Add nuget package source

I did the mistake of entering my github password as password. Instead you should create a token under your github settings as shown below and enter the token itself instead of your account's password.

Generate token

In my case, I selected the following options when creating the token

Token options

VS 19 unfortunately did not prompt another time for credentials and kept showing the error message:

[github] Failed to retrieve metadata from source ... .Response status code does not indicate success: 401 (Unauthorized).

To change this parameters on Windows, open Control Panel\User Accounts\Credential Manager as shown below:

Web credentials

Click on Edit, and paste the access token you just created on github as password. Scroll down the list of credentials. In my case, I needed to change the password in a second place as well.

Second web credentials

dhilmathy
  • 2,800
  • 2
  • 21
  • 29
aurel-LX
  • 189
  • 2
  • 6