Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use credentials in HttpClient in c#?

I am facing some problems when using the HttpClient class to access to a Delicious API. I have the following code:

try
{
    const string uriSources = "https://api.del.icio.us/v1/tags/bundles/all?private={myKey}";
    using (var handler = new HttpClientHandler { Credentials = new  
                             NetworkCredential("MyUSER", "MyPASS") })
    {
         using (var client = new HttpClient(handler))
         {
             var result = await client.GetStringAsync(uriSources);
         }
   }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "ERROR...", MessageBoxButton.OK);
}

When running the code above I am getting the following: Response status code does not indicate success: 401 (Unauthorized).

So, how could I get this work? Is it possible?

Thanks in advance

Regards!

like image 402
MikePR Avatar asked Aug 28 '13 02:08

MikePR


People also ask

How do I pass credentials to HttpClient?

Let's start with the standard way of configuring Basic Authentication on the HttpClient – via a CredentialsProvider: CredentialsProvider provider = new BasicCredentialsProvider(); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user1", "user1Pass"); provider. setCredentials(AuthScope.

What is the use of HttpClient in C#?

The HttpClient class instance acts as a session to send HTTP requests. An HttpClient instance is a collection of settings applied to all requests executed by that instance. In addition, every HttpClient instance uses its own connection pool, isolating its requests from requests executed by other HttpClient instances.

What happens if you don't dispose HttpClient?

There is no need to dispose of the HttpClient instances from HttpClientFactory. Disposal will not actually do anything in this case because the factory manages the handler and connection lifetimes and not the HttpClient instances.


Video Answer


2 Answers

I had the exact same problem myself. It seems the HttpClient just disregards the credentials set in the HttpClientHandler.

The following shall work however:

using System.Net.Http.Headers; // For AuthenticationHeaderValue

const string uri = "https://example.com/path?params=1";
using (var client = new HttpClient()) {
    var byteArray = Encoding.ASCII.GetBytes("MyUSER:MyPASS");
    var header = new AuthenticationHeaderValue(
               "Basic", Convert.ToBase64String(byteArray));
    client.DefaultRequestHeaders.Authorization = header;

    var result = await client.GetStringAsync(uri);
}

No need for the handler.

Source: http://www.snip2code.com/Snippet/13895/Simple-C---NET-4-5-HTTPClient-Request-Us

like image 138
Adam Szabo Avatar answered Oct 02 '22 06:10

Adam Szabo


This is an old post but thought to add my reply for someone facing similar issue and browsing answers...

I faced similar issue. In my case, setting Domain property for NetworkCredentials worked. You can try setting Domain.

like image 40
Nirav Darji Avatar answered Oct 02 '22 06:10

Nirav Darji