设置使用的NuGet核心DLL包凭证 [英] Setting the Package Credentials using Nuget Core DLL

查看:239
本文介绍了设置使用的NuGet核心DLL包凭证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在使用HTTP Autentication私人给料包的列表。这是我的代码,当我打电话的ListPlugins方法,我得到一个401错误,我怎么能设置凭据?



 公共类插件管理
{
私人只读字符串_pluginFolder;
私人只读IPackageRepository _packageRepository;
私人只读软件包管理系统_packageManager;

公共插件管理(字符串plugInFolder,串packageRepositoryAddres)
{
_pluginFolder = plugInFolder;
_packageRepository = PackageRepositoryFactory.Default.CreateRepository(packageRepositoryAddres);
_packageManager =新的软件包管理系统(_packageRepository,_pluginFolder);
}

公开的IEnumerable< PluginModel> ListPlugins()
{
IPackage哑= NULL;

VAR的结果= _packageManager.SourceRepository.GetPackages()
.OrderBy(P => p.Id)
.ToList()
。选择(P = >新建PluginModel()
{
包标识= p.Id,
PackageVersion = p.Version.ToString(),
PackageDescription = p.Description,
IsInstalled的= _packageManager.LocalRepository.TryFindPackage(p.Id,p.Version,走出虚拟)
})
.ToList();

返回结果;
}

公共无效安装(串包标识,串packageVersion)
{
_packageManager.InstallPackage(包标识,新SemanticVersion(packageVersion));
}

公共无效卸载(串包标识,串packageVersion)
{
_packageManager.UninstallPackage(包标识,新SemanticVersion(packageVersion));
}
}


解决方案

一办法做到这一点,这是在的NuGet Visual Studio和SharpDevelop的工程,是实现自己的ICredentialProvider或使用SettingsCredentialProvider类是提供NuGet.Core。该设置凭据提供将读取一个文件NuGet.config任何凭据



例如,在SharpDevelop的和MonoDevelop的下面的代码使用设置提供者和自定义提供:

 静态无效InitializeCredentialProvider()
{
ISettings设置= Settings.LoadDefaultSettings(NULL,NULL,NULL) ;
变种packageSourceProvider =新PackageSourceProvider(设置);
变种credentialProvider =新SettingsCredentialProvider(新SharpDevelopCredentialProvider(),packageSourceProvider);

HttpClient.DefaultCredentialProvider = credentialProvider;
}



自定义凭据提供,至少在SharpDevelop的什么也不做目前,在Visual Studio它会提示他们的凭据用户。你可以忽略的设置提供者,只是使用自定义凭据提供商,而不是。当前实现在SharpDevelop的凭证提供者是:



 公共类SharpDevelopCredentialProvider:ICredentialProvider 
{
公ICredentials getCredentials(显示乌里URI,IWebProxy代理,credentialType credentialType,布尔重试)
{
返回NULL;
}
}



所以,你可以有你的凭证从GetCredentials方法返回在自定义凭据提供程序类。



提供商需要对HttpClient的设置。您正在使用PackageRepositoryFactory类,以便将使用HttpClient的,如果你的软件包源是一个URL,而不是一个文件。


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));
    }
}

解决方案

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.

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;
    }

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;
    }
}

So you could have your credentials returned from the GetCredentials method in your custom credential provider class.

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核心DLL包凭证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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