If you have been using dotnet RC2 with private NuGet feeds such as those offered by VSTS, you might have come across a bug during package restore for automated builds.
When working in Visual Studio, the package manager UI handles the authentication for us.
If you need to restore on an automated build, you'll get an error reading Unauthorized (401). This is explained in the following github issues:
There are more discussions and the issue is pending a fix. The workaround is to put a clear text password in your NuGet.config file as described here. This is a bad practice as you do not commit passwords/secrets in the source.
As a workaround, I made a script that would add the credentials to an existing NuGet.config before performing
dotnet restore. The script is below.
param($file, $feedName, $uname, $pass) $xml =[xml](Get-Content $file) # intention is to have # <configuration> # <packageSourceCredentials> # <feedName> # <add key="Username" value="[uname] /> # <add key="ClearTextPassword" value="[pass]" /> # </feedName> # </packageSourceCredentials> # </configuration> # create the username node and set the attributes $userNameNode = $xml.CreateElement("add") $userNameNode.SetAttribute("key", "Username") $userNameNode.SetAttribute("value", $uname) # create the password node and set the attributes $passwordNode = $xml.CreateElement("add") $passwordNode.SetAttribute("key", "ClearTextPassword") $passwordNode.SetAttribute("value", $pass) # create the feedName node and attach the username and password nodes $feedNameNode = $xml.CreateElement($feedName) [void] $feedNameNode.AppendChild($userNameNode) [void] $feedNameNode.AppendChild($passwordNode) # create the packageSourceCredentials node and append the feedName node $credentialsNode = $xml.CreateElement("packageSourceCredentials") [void] $credentialsNode.AppendChild($feedNameNode); # add the packageSourceCredentials node to the document's configuration node $xml.configuration.AppendChild($credentialsNode); # save the file to the same location $xml.Save($file)
To use, it, add it as a step before you build your project as shown below.
Before invoking the command, you must have a pair of a username and password used to access the feed.
If you are using the package feature available in VSTS, you need to create a personal access token as described here.
The command is invoked with the following arguments:
-file $(Build.SourcesDirectory)\NuGet.config -feedName "feedName" -uname "feedUserName" -pass "<feedPassword>"
This will result in a NuGet.config file similar to this one.
The one big advantage is that the credentials will never be in the source code.