Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用Cake build对安全的NuGet服务器进行身份验证?_C#_Msbuild_Nuget_Build Server_Cakebuild - Fatal编程技术网

C# 如何使用Cake build对安全的NuGet服务器进行身份验证?

C# 如何使用Cake build对安全的NuGet服务器进行身份验证?,c#,msbuild,nuget,build-server,cakebuild,C#,Msbuild,Nuget,Build Server,Cakebuild,我们正在使用Cake Build自动化我们的构建,我们使用NuGet.org上的NuGet包,但我们也有自己的NuGet Feed服务器,该服务器具有用户名/密码身份验证来访问。我们如何将Cake Build与带有身份验证的自定义NuGet提要服务器结合使用?Cake使用NuGet.exe安装工具、加载项和NuGet别名 除非您在#工具/#addin指令中指定了源代码或为NuGet别名提供了源代码,否则NuGet.exe将在当前路径中查找NuGet.config,最终以当前用户全局设置结束(%A

我们正在使用Cake Build自动化我们的构建,我们使用NuGet.org上的NuGet包,但我们也有自己的NuGet Feed服务器,该服务器具有用户名/密码身份验证来访问。我们如何将Cake Build与带有身份验证的自定义NuGet提要服务器结合使用?

Cake使用
NuGet.exe
安装工具、加载项和NuGet别名

除非您在
#工具
/
#addin
指令中指定了源代码或为NuGet别名提供了源代码,否则
NuGet.exe
将在当前路径中查找
NuGet.config
,最终以当前用户全局设置结束(
%AppData%\NuGet\NuGet.config

您有几个选项,如果您不想更改Cake文件或存储库中的任何内容,则可以全局存储您的用户凭据,
NuGet.exe
将选择以下示例:

nuget sources Update -Name [name of source] -Source [uri to your source] -UserName [your username] -Password [your password]
免责声明某些版本的
NuGet.exe
和dotnet CLI存在加密密码问题,解决方法是添加
-StorePasswordInClearText
,如下所示:

然后,您的凭证将以纯文本形式保存,这与您的凭证以纯文本形式保存的缺点相匹配

您还可以通过指定
#工具
/
#addin
指令和nuget别名的特定源来覆盖
nuget.config
设置

#工具指令 下面是为
#tool
指令提供源代码的示例

#tool "NUnit.ConsoleRunner"
or
#tool nuget:?package=NUnit.ConsoleRunner&version=3.4.0
#addin "Cake.Slack"
or
#addin nuget:?package=Cake.Slack&version=0.4.0
变成

#tool nuget:[source]?package=NUnit.ConsoleRunner
or
#tool nuget:[source]?package=NUnit.ConsoleRunner&version=3.4.0
#addin nuget:[source]?package=Cake.Slack
or
#addin nuget:[source]?package=Cake.Slack&version=0.4.0
也就是说,对于官方V2 nuget提要

#tool nuget:https://www.nuget.org/api/v2?package=NUnit.ConsoleRunner
or
#tool nuget:https://www.nuget.org/api/v2?package=NUnit.ConsoleRunner&version=3.4.0
#addin nuget:https://www.nuget.org/api/v2?package=Cake.Slack
or
#addin nuget:https://www.nuget.org/api/v2?package=Cake.Slack&version=0.4.0
#addin指令 下面是为
#addin
指令提供源代码的示例

#tool "NUnit.ConsoleRunner"
or
#tool nuget:?package=NUnit.ConsoleRunner&version=3.4.0
#addin "Cake.Slack"
or
#addin nuget:?package=Cake.Slack&version=0.4.0
变成

#tool nuget:[source]?package=NUnit.ConsoleRunner
or
#tool nuget:[source]?package=NUnit.ConsoleRunner&version=3.4.0
#addin nuget:[source]?package=Cake.Slack
or
#addin nuget:[source]?package=Cake.Slack&version=0.4.0
也就是说,对于官方V2 nuget提要

#tool nuget:https://www.nuget.org/api/v2?package=NUnit.ConsoleRunner
or
#tool nuget:https://www.nuget.org/api/v2?package=NUnit.ConsoleRunner&version=3.4.0
#addin nuget:https://www.nuget.org/api/v2?package=Cake.Slack
or
#addin nuget:https://www.nuget.org/api/v2?package=Cake.Slack&version=0.4.0
NuGet别名 NuGet具有诸如和之类的命令,用于直接处理源代码。例如,如果您希望在NuGet还原步骤之前将源代码添加到CI中,这些命令非常有用,如下所示:

var source = new {
                Name = EnvironmentVariable("PRIVATE_FEED_NAME"),
                Source = EnvironmentVariable("PRIVATE_FEED_SOURCE"),
                ApiUserName = EnvironmentVariable("PRIVATE_FEED_USERNAME"),
                ApiKey = EnvironmentVariable("PRIVATE_FEED_PASSWORD")
             };

if (!NuGetHasSource(source.SourceUrl))
{
    NuGetAddSource(
        source.Name,
        source.SourceUrl,
        new NuGetSourcesSettings {
            UserName = source.ApiUserName,
            Password = source.ApiKey
        }
    );
}
以上内容只会将源代码添加到现有的
nuget.config
,但您也可以覆盖&alias的nuget源代码

NuGetInstall NuGetInstall别名具有采用工具设置类的重载,该工具设置类具有一个属性,可用于覆盖使用的提要,例如:

NuGetInstall("MyNugetPackage", new NuGetInstallSettings {
    Source = new []{ "https://api.nuget.org/v3/index.json" }
});
var solutions = GetFiles("./**/*.sln");
// Restore all NuGet packages.
foreach(var solution in solutions)
{
    Information("Restoring {0}", solution);
    NuGetRestore(
        solution,
        new NuGetRestoreSettings {
            Source = new []{ "https://api.nuget.org/v3/index.json" }
        }
    );
}
NuGetRestore 类似地,NuGetRestore别名具有重载,允许您指定一个属性,该属性可用于覆盖使用的提要,例如:

NuGetInstall("MyNugetPackage", new NuGetInstallSettings {
    Source = new []{ "https://api.nuget.org/v3/index.json" }
});
var solutions = GetFiles("./**/*.sln");
// Restore all NuGet packages.
foreach(var solution in solutions)
{
    Information("Restoring {0}", solution);
    NuGetRestore(
        solution,
        new NuGetRestoreSettings {
            Source = new []{ "https://api.nuget.org/v3/index.json" }
        }
    );
}
结论 有几种方法可以解决您的问题

当您的计算机上配置了多个源时,您还可以通过指定一个源来提高NuGet restore/install的性能,但当前项目仅使用正式源,因为它会跳过所有配置的源的查看并直接访问源

但是,如果您的订阅源具有身份验证,那么您需要为使用
nuget.exe
或别名的用户添加凭据

提示:对于使用它的用户,它有url:s,您可以在不添加源的情况下使用,但只需指定还原/安装的源属性,这是敏感信息,因此不要将它们存储在构建脚本中,而是作为环境变量存储