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

3
  • What is .netstandard? I have only heard about .NET Framework Commented Aug 22, 2018 at 5: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. Commented Aug 22, 2018 at 6: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 Commented Aug 22, 2018 at 7:25

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

3 Comments

I have tried this as well. with the only change in the last line. client.SendAsync(requestMessage).Result. It's not working either.
the code you posted above has different argument in AuthenticationHeaderValue constructor from what I have given in my answer
Sorry. Typo in my post. In code i am using exactly requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", your_token); which is not working.
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>");

Comments

-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

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.