访问Nuget提要时如何传递凭据 [英] How to pass credentials when accessing a Nuget feed

查看:224
本文介绍了访问Nuget提要时如何传递凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Nuget存储库下载软件包,该存储库需要使用NuGet.Core访问凭证.

I am trying to download packages from a Nuget repository which requires credentials for it to be accessed using NuGet.Core.

我知道可以通过以下方式访问没有身份验证的Nuget存储库:

I know that Nuget repositories with no authentication can be accessed as follows:

//ID of the package to be looked up
string packageID = "EntityFramework";

//Connect to the official package repository
IPackageRepository repo = PackageRepositoryFactory.Default.CreateRepository("https://packages.nuget.org/api/v2");

//Initialize the package manager
string path = <PATH_TO_WHERE_THE_PACKAGES_SHOULD_BE_INSTALLED>
PackageManager packageManager = new PackageManager(repo, path);

//Download and unzip the package
packageManager.InstallPackage(packageID, SemanticVersion.Parse("5.0.0"));

(来源: Nuget博客)

现在假设我具有Nuget存储库的凭据. 我尝试使用http客户端和网络凭据来获取软件包,但是它不起作用. 那么如何将凭据传递到Nuget服务器?

Now suppose I have the credentials of a Nuget repository. I tried to get the package using http client and network credentials but it did not work. So how do I pass the credentials along to the Nuget server?

我还想知道如何限制对Nuget的访问feed (较新的版本提供什么?).

Also I would like to know how to restrict access to a Nuget feed(what do newer versions offer?).

我似乎找不到任何清晰的文档.

I can't seem to find any clear documentation.

谢谢.

推荐答案

我假设您使用的是NuGet 2.x,而不是NuGet 3.0.

I am assuming you are using NuGet 2.x and not NuGet 3.0.

为了使NuGet将凭据发送到服务器,您需要在NuGet的HttpClient类上配置DefaultCredentialProvider.

In order for NuGet to send credentials to a server you need to configure the DefaultCredentialProvider on NuGet's HttpClient class.

HttpClient.DefaultCredentialProvider = new YourCredentialProvider ();

NuGet提供一个SettingsCredentialProvider类,该类将尝试在用户的NuGet.Config文件中查找用户名和密码,但是您仍然需要实现自己的ICredentialProvider,因为SettingsCredentialProvider需要将其传递给其构造函数.它通常使用此凭据提供程序来提示用户输入凭据.

NuGet provide a SettingsCredentialProvider class which will try to find username's and password's in the user's NuGet.Config file but you still need to implement your own ICredentialProvider since the SettingsCredentialProvider requires one to be passed to its constructor. It uses this credential provider typically to prompt the user to enter credentials.

HttpClient.DefaultCredentialProvider = new SettingsCredentialProvider(new ConsoleCredentialProvider(Console), SourceProvider, Console);

NuGet提供您可以使用的ConsoleCredentialProvider.它还提供了控制台和创建源提供者的方法.

NuGet provides a ConsoleCredentialProvider which you could use. It also provides a Console and a way to create a source provider.

要限制对您自己的NuGet服务器的访问,可以使用NuGet.Server NuGet包并创建自己的Web应用程序.然后,您可以将其托管在IIS上并使用标准的IIS身份验证功能.

To restrict access to your own NuGet server you can use the NuGet.Server NuGet package and create your own web app. Then you can host that on IIS and use the standard IIS authentication features.

一种替代方法是使用第三方托管服务,例如 MyGet ,该服务允许您设置需要身份验证的私人供稿.

An alternative is to use a third party hosting service such as MyGet which allows you to setup private feeds that require authentication.

这篇关于访问Nuget提要时如何传递凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆