问题描述
我想通过 Http 身份验证获取私有提要中的包列表.这是我的代码,当我调用 ListPlugins 方法时出现 401 错误,我该如何设置凭据?
I want to get a list of package in a private feed with Http Autentication. This is my code, when I call the ListPlugins Method I get a 401 error, how can I set the credentials?
public class PluginManager
{
private readonly string _pluginFolder;
private readonly IPackageRepository _packageRepository;
private readonly PackageManager _packageManager;
public PluginManager(string plugInFolder, string packageRepositoryAddres)
{
_pluginFolder = plugInFolder;
_packageRepository = PackageRepositoryFactory.Default.CreateRepository(packageRepositoryAddres);
_packageManager = new PackageManager(_packageRepository, _pluginFolder);
}
public IEnumerable<PluginModel> ListPlugins()
{
IPackage dummy = null;
var result = _packageManager.SourceRepository.GetPackages()
.OrderBy(p => p.Id)
.ToList()
.Select(p => new PluginModel()
{
PackageId = p.Id,
PackageVersion = p.Version.ToString(),
PackageDescription = p.Description,
IsInstalled = _packageManager.LocalRepository.TryFindPackage(p.Id, p.Version, out dummy)
})
.ToList();
return result;
}
public void Install(string packageId, string packageVersion)
{
_packageManager.InstallPackage(packageId, new SemanticVersion(packageVersion));
}
public void Uninstall(string packageId, string packageVersion)
{
_packageManager.UninstallPackage(packageId, new SemanticVersion(packageVersion));
}
}
推荐答案
实现这一点的一种方法是实现您自己的 ICredentialProvider 或使用 NuGet 中可用的 SettingsCredentialProvider 类,这就是 Visual Studio 和 SharpDevelop 中的 NuGet.核.设置凭据提供程序将读取 NuGet.config 文件中的所有凭据.
One way to do this, which is what NuGet in Visual Studio and SharpDevelop works, is to implement your own ICredentialProvider or use the SettingsCredentialProvider class that is available in NuGet.Core. The settings credential provider will read any credentials in a NuGet.config file.
例如,在 SharpDevelop 和 MonoDevelop 中,以下代码使用设置提供程序和自定义提供程序:
For example, in SharpDevelop and MonoDevelop the following code uses the settings provider and a custom provider:
static void InitializeCredentialProvider()
{
ISettings settings = Settings.LoadDefaultSettings(null, null, null);
var packageSourceProvider = new PackageSourceProvider(settings);
var credentialProvider = new SettingsCredentialProvider(new SharpDevelopCredentialProvider(), packageSourceProvider);
HttpClient.DefaultCredentialProvider = credentialProvider;
}
自定义凭据提供程序,至少在 SharpDevelop 中目前什么都不做,在 Visual Studio 中它会提示用户输入他们的凭据.您可以忽略设置提供程序,而只使用自定义凭据提供程序.SharpDevelop 中凭据提供程序的当前实现是:
The custom credential provider, at least in SharpDevelop does nothing currently, in Visual Studio it prompts the user for their credentials. You could ignore the settings provider and just use a custom credential provider instead. The current implementation for the credential provider in SharpDevelop is:
public class SharpDevelopCredentialProvider : ICredentialProvider
{
public ICredentials GetCredentials(Uri uri, IWebProxy proxy, CredentialType credentialType, bool retrying)
{
return null;
}
}
因此,您可以从自定义凭据提供程序类中的 GetCredentials 方法返回您的凭据.
So you could have your credentials returned from the GetCredentials method in your custom credential provider class.
需要在 HttpClient 上设置提供程序.您正在使用 PackageRepositoryFactory 类,因此如果您的包源是 url 而不是文件,它将使用 HttpClient.
The provider needs to be set on the HttpClient. You are using PackageRepositoryFactory class so that will use the HttpClient if your package source is a url and not a file.
这篇关于使用 Nuget Core DLL 设置包凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!