Skip to content

Instantly share code, notes, and snippets.

@xavierdecoster
Last active July 12, 2022 12:43
Show Gist options
  • Save xavierdecoster/3205826 to your computer and use it in GitHub Desktop.
Save xavierdecoster/3205826 to your computer and use it in GitHub Desktop.
Store MyGet credentials in your roaming user profile NuGet.config

Execute the following script using your MyGet [feedUrl] and MyGet [username] , [password] and [apikey]. Run this from a commandline where you have access to nuget.exe (or set the path to your nuget.exe in a system environment variable).

Store credentials in machine-level nuget.config (non-transferable)

nuget setapikey [apikey] -source [feedUrl]
nuget sources add|update -Name [name] -source [feedUrl] -User [username] -pass [password]

Store credentials in specific nuget.config (non-transferable)

nuget setapikey [apikey] -source [feedUrl] -configFile [configFilePath]
nuget sources add|update -Name [name] -source [feedUrl] -User [username] -pass [password] -configFile [configFilePath]

Store credentials in specific nuget.config (transferable)

Don't bother putting the above nuget.config files into source control as they'll only work on the machine and the account that created the configs.

To be able to share credentials, use the -StorePasswordInClearText option:

nuget setapikey [apikey] -source [feedUrl] -configFile [configFilePath]
nuget sources add|update -Name [name] -source [feedUrl] -User [username] -pass [password] -configFile [configFilePath] -StorePasswordInClearText
@dotnetchris
Copy link

So by following this gist, this information is bounded to the machine-level nuget.config. Following this having the same feed url inside VS or explicitly calling it from nuget.exe, it will automatically resolve that url matches one in the config and pick up the credentials from it?

@xavierdecoster
Copy link
Author

Actually it is machine-level for the given user account you run these commands under. That's why it is important for build servers to run these under the build service account (cfr my post on debugging package restore: http://www.xavierdecoster.com/debugging-nuget-package-restore

Both the nuget.exe command line as well as the NuGet Visual Studio extension use the information stored in this config, so they'll pick up these credentials.

@nul800sebastiaan
Copy link

So currently I've added the following in my NuGet.targets file:

    <PropertyGroup>
        <RemoveMyCredentialsCommand>$(NuGetCommand) sources remove -Name MyPrivateFeed</RemoveMyCredentialsCommand>
        <AddMyCredentialsCommand>$(NuGetCommand) sources add -Name MyPrivateFeed -source http://www.myget.org/F/MyPrivateFeed -UserName myUser -Password myPass</AddMyCredentialsCommand>       
    </PropertyGroup>

  <Exec Command="$(RemoveMyCredentialsCommand)" 
   IgnoreStandardErrorWarningFormat="true"
   IgnoreExitCode="true"
   ContinueOnError="WarnAndContinue" />     

  <Exec Command="$(AddMyCredentialsCommand)"
   IgnoreStandardErrorWarningFormat="true"
   IgnoreExitCode="true"
   ContinueOnError="WarnAndContinue" />

That works fine for VS and TeamCity.

Now, I'm trying to do the same with Windows Azure Websites, using Git deploys but it doesn't want to execute this properly, the command exited with code 1, but since I have no control over the environment I don't know what went wrong exactly.

Unable to find any package source(s) matching name: MyPrivateFeed.
C:\DWASFiles\Sites\someSite\VirtualDirectory0\site\repository.nuget\nuget.targets(91,7): warning : "DeleteSection" cannot be called on a NullSettings. [C:\DWASFiles\Sites\someSite\VirtualDirectory0\site\repository\My.Site.Project\My.Site.Project.csproj]
C:\DWASFiles\Sites\someSite\VirtualDirectory0\site\repository.nuget\nuget.targets(91,7): warning MSB3073: The command ""C:\DWASFiles\Sites\someSite\VirtualDirectory0\site\repository.nuget\nuget.exe" sources add -Name MyPrivateFeed -source http://www.myget.org/F/MyPrivateFeed -UserName myUser -Password myPass" exited with code 1. [C:\DWASFiles\Sites\someSite\VirtualDirectory0\site\repository\My.Site.Project\My.Site.Project.csproj]

@nul800sebastiaan
Copy link

Ps. This works fine for TeamCity. Also, I've previously found a setup where someone changed their NuGet.config file to include , and . Should I be looking into those?

@xavierdecoster
Copy link
Author

Yeah, it's hacking around for now, you might be interested in this post to see how I got it to work on TFS Online (warning: monster hack!) http://www.xavierdecoster.com/nuget-tfs-preview-a-challenging-combination

@nul800sebastiaan
Copy link

Hey Xavier, thanks for your hints.

Unfortunately, WAWS doesn't allow me to copy things into the User folder, so I couldn't get this bird to sing. Here's hoping for improvements to NuGet in the future. For now I've put all the packages I could in a public repo and included one binary dependency in source control.

Thanks again!

@xavierdecoster
Copy link
Author

Updated Gist using the new -StorePasswordInClearText and -ConfigFile options

@paumayr
Copy link

paumayr commented Feb 6, 2014

Sorry, for over complicating, but I'm not to familiar with the github gist / git fork / pull request issues yet, but I have a small fix for your gist here:
https://gist.github.com/paumayr/8845667

@xavierdecoster
Copy link
Author

Hi @paumayr, you can click the fork button on top of this page, make changes to your fork, and submit a PR, should be easy :) Otherwise, would you mind highlighting what changes you did?
Thanks!

@Qianfinland
Copy link

This is what exactly I need, thank you xavierdecoster:) One thing I know that after you store the myget credential in the global NuGet.Config file and the window asking your password still prompt up. You could try Run as administrator when open your Visual Studio

@michaelaird
Copy link

I'm trying this solution to work around the bug in TeamCity/Nuget around the authenticated feed but it still doesn't seem to be working. What is the right directory to put the nuget.config file in? Is it case sensitive (NuGet.config vs nuget.config)? does the source URL need to match the "internal"/localhost url of the feed? trailing / or no trailing /?

so many questions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment