[英]NuGet Client SDK - Connecting to a private NuGet feed through code
我正在通過 Azure DevOps 使用私有 NuGet 提要。
要從代碼中查看有關公共NuGet 包的信息,只需遵循此處的文檔即可。 特別是:
ILogger logger = NullLogger.Instance;
CancellationToken cancellationToken = CancellationToken.None;
SourceCacheContext cache = new SourceCacheContext();
SourceRepository repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
PackageMetadataResource resource = await repository.GetResourceAsync<PackageMetadataResource>();
IEnumerable<IPackageSearchMetadata> packages = await resource.GetMetadataAsync(
"Newtonsoft.Json",
includePrerelease: true,
includeUnlisted: false,
cache,
logger,
我試圖通過將我的憑據添加到流程中來為我自己的私人提要實現相同的結果:
var logger = NullLogger.Instance;
var cancellationToken = CancellationToken.None;
var cache = new SourceCacheContext();
var source = new PackageSource("*my feed*/nuget/v3/index.json");
source.Credentials = new PackageSourceCredential("*my feed*/nuget/v3/index.json", "*username*", "*password*", true, null);
var repository = Repository.Factory.GetCoreV2(source);
var resource = await repository.GetResourceAsync<PackageMetadataResource>();
IEnumerable<IPackageSearchMetadata> searchMetadata = await resource.GetMetadataAsync(
"My.Package.Name",
includePrerelease: true,
includeUnlisted: false,
cache,
logger,
cancellationToken);
請注意,即使我調用Factory.GetCoreV2()
,該方法的內部仍會調用 V3 提供程序。 它只是將 PackageSource 作為參數的唯一構造函數:
public static SourceRepository GetCoreV2(this Repository.RepositoryFactory factory, PackageSource source)
{
return Repository.CreateSource(Repository.Provider.GetCoreV3(), source);
}
然而,無論我做什么,我都無法通過步驟
var resource = await repository.GetResourceAsync<PackageMetadataResource>();
它不斷失敗並出現NuGet.Protocol.Core.Types.FatalProtocolException - 401 Unauthorised
。 我已經驗證了我的憑據和我的提要端點的有效性,以及我在提要中的權限(完全訪問)。 我也試過這里描述的初始化過程(加上我的憑據的步驟),但結果是一樣的。
我應該如何從代碼中識別 Azure DevOps 源,以便能夠獲取 IPackageMetadata 信息?
Azure DevOps 不支持通過 NuGet 進行用戶名和密碼身份驗證。 如果您的帳戶需要 2 因素身份驗證 (2FA),則 NuGet 無法提示您進行驗證。 我不知道他們是否決定不支持所有帳戶,即使是未啟用 2FA 的帳戶,或者是否還有其他原因(我懷疑出於其他安全原因不允許這樣做)。
如果您安裝了 Visual Studio 或以其他方式安裝了Azure Artifacts NuGet 憑據提供程序,也許您可以嘗試在代碼中不使用任何憑據。 我不確定 nuget.exe、dotnet cli 和 msbuild 究竟是如何發現憑據提供程序的,但也許它在使用 sdk 時也能工作。
否則,如果您想或需要使用憑據 API,則需要生成個人訪問令牌,並將其用作密碼。
我挖得更深一些,似乎確實有可能,請參閱此 Gist 和示例實現https://gist.github.com/cpyfferoen/74092a74b165e85aed5ca1d51973b9d2
管理安全 DevOps 包源的另一種方法是使用 DevOps REST API https://docs.microsoft.com/en-us/rest/api/azure/devops/artifacts/feed%20%20management/get%20feeds?視圖=azure-devops-rest-6.0
似乎沒有我可以看到的與此 API 接口的包,因此我創建了一個非常簡單的庫,它使用了一些 REST API 方法
https://github.com/conficient/DevOpsNugetClient
我用它成功地查詢和刪除了我們內部提要上的數百個冗余包和相關版本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.