13

I'm trying to setup a web api using ASP.Net Core 6 so that users can hit my end points and then I do some work in D365 behind the scenes using a privileged account. I'm using a typed HTTP Client, but I'm not sure how to plugin the bearer authentication so that all the requests from this client have the correct Authorization header attached.

Program.cs

builder.Services.AddHttpClient<D365Service>();

D365Service.cs

private readonly HttpClient httpClient;

public D365Service(HttpClient httpClient)
{
  this.httpClient = httpClient;

  this.httpClient.DefaultRequestHeaders.Add("Accept", "*/*");
  this.httpClient.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br");
  // a whole bunch of other headers
  // Is this where I can add in the bearer Authorization header? How do I generate that token?
}

Any help is appreciated. Thanks.

4
  • 1
    "How do I generate that token?" - you generate that token by following the instructions of the service that you're connecting to. Commented Apr 19, 2022 at 5:01
  • 1
    Adding http request header, you may refer to this answer. Commented Apr 19, 2022 at 6:25
  • 1
    That doesn't look like a named client to me, that's a typed client ... (learn.microsoft.com/en-us/aspnet/core/fundamentals/…) AddHttpClient registers a transient service, but the answer to your question would depend on the lifecycle of your tokens and who should use them. Commented Apr 20, 2022 at 5:02
  • @JeremyLakeman - you're right. I'm using that documentation you linked as a guide. I just used the wrong name. I've updated my post. Commented Apr 20, 2022 at 15:39

2 Answers 2

19

To add the token to your httpclinet

you should use the following code

httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer",accessToken);

How do I generate that token

as @DiplomacyNotWar explained in his comment you should be able to generate that token by following the instructions of the service you are connecting to

Some services will share user name and password ( app Id & secret Key ) and you could use this to set your basic Authentication then you will be able to call your token end point that return the access token that you will be able to use it with your httpclinet

Regards,

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

2 Comments

Thanks. I can get the bearer token in Postman so I guess I got to use the httpClient in my constructor to make the same call then capture the token and add it to the header. I'll give that a shot.
You could create a function where you get the token then set the header as explained above and call this function before you call the http client , Don't add it to the constructor since you will need to async await it If that answer your question could you mark my answer as accepted answer
4

I know 2 ways to add token to HttpClient. But I don't know what's the difference between them

client.DefaultRequestHeaders.Add("Authorization", "Bearer " + #YourToken);

And

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", #YourToken);

they work well. but I think they will have something different. if you know, please show me

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.