3

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

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.