0

I am trying to do a GET call using HttpClient with Authorization header. [Authorization: Bearer ].

The HttpClient.GetAsync always returns 401 (unauthorized). When the same call is made using .netstarndard's HttpClient it works fine. Below is the code which shows me 401. The same endpoint works in Postman as well. Please help me on what i am missing.

HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("GET"), "<Get endpoint>");
request.Headers.Add("Authorization", "Bearer " + "<token>");
var resp = client.SendAsync(request).Result;

I also tried with the below code as well, which is also not working.

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "<token>");
client.GetAsync("<get endpoint>");

And with this.

HttpClient.DefaultRequestHeaders.Add("Authorization", "Bearer <token>");

Note: The .netstandard HttpClient works with the same code. Problem is only with .NET Framework. [Please don't suggest me to use .netstarndard library as I want to call the HttpClient Get from my .NET Framework application.]

I am trying to call OAuth's identity/connect/userinfo endpoint. Don't know if it makes any difference.

Please help. Thanks in Advance, Kannan

Kannan Mohan
  • 67
  • 1
  • 11
  • What is .netstandard? I have only heard about .NET Framework – Ankush Jain Aug 22 '18 at 05:14
  • The HttpClient from C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\System.Net.Http.dll is not work. But the HttpClient comes with C:\Users\xxxxx\.nuget\packages\netstandard.library\2.0.1\build\netstandard2.0\ref\netstandard.dll works good. Not sure what I am missing in .Net framework. – Kannan Mohan Aug 22 '18 at 06:57
  • @AnkushJain - a question that is trivially answered with a good search engine. As a concept, it's existed for a couple of years already. Microsoft has plenty of [documentation](https://learn.microsoft.com/en-us/dotnet/standard/net-standard) – Damien_The_Unbeliever Aug 22 '18 at 07:25

3 Answers3

2

Try this way:

HttpClient client = new HttpClient();
var requestMessage = new HttpRequestMessage(HttpMethod.Get, "https://your.site.com");
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", your_token);
client.SendAsync(requestMessage);
Ankush Jain
  • 5,654
  • 4
  • 32
  • 57
  • I have tried this as well. with the only change in the last line. `client.SendAsync(requestMessage).Result`. It's not working either. – Kannan Mohan Aug 22 '18 at 06:02
  • the code you posted above has different argument in AuthenticationHeaderValue constructor from what I have given in my answer – Ankush Jain Aug 22 '18 at 06:07
  • Sorry. Typo in my post. In code i am using exactly `requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", your_token);` which is not working. – Kannan Mohan Aug 22 '18 at 06:41
0

You said you tried

HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Authorization", "<token>"); client.GetAsync("<get endpoint>");

Here you pass "Authorization" to the AuthenticationHeaderValue, which I think is mistake. You should pass the scheme to AuthenticationHeaderValue not the header name.So "Bearer" in your case. Try this HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "<token>"); client.GetAsync("<get endpoint>");

D.Dimov
  • 53
  • 1
  • 4
-1

I will recommend you to use Http Client Wrapper as shown below in the url. There are example methods which you can use.

https://github.com/elgunmirze/HttpClientWrapper