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?<
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.