Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the Package Credentials using Nuget Core DLL

Tags:

c#

nuget

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));
    }
}
like image 285
Jedi Master Spooky Avatar asked Mar 24 '26 04:03

Jedi Master Spooky


1 Answers

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.

like image 90
Matt Ward Avatar answered Mar 26 '26 18:03

Matt Ward



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!