Hi
I want to know How to consume oauth2 api using asp.net core.
I want to call third party API in asp.net core. I want to know where can i set all my config files like base url, client id,secret key in .Net core
regards,
Manish
Hi
I want to know How to consume oauth2 api using asp.net core.
I want to call third party API in asp.net core. I want to know where can i set all my config files like base url, client id,secret key in .Net core
regards,
Manish
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Prasad RaveendranPosted Jan 13, 2024, 5:43 PM
In .NET 6.0, the startup configuration has been simplified with the introduction of the simplified hosting model. The
Startup.csfile is not required by default, but you can still use it if you prefer. However, the convention has shifted towards using theProgram.csfile for configuration.Here's how you can set up the API consumption using OAuth2 in .NET 6.0:
Create Configuration Model:
Create a model to represent the configuration values for the OAuth2 API. For example:
Configure AppSettings.json:
Add the API configuration values to your
appsettings.jsonfile:Configure Program.cs:
In the
CreateHostBuildermethod ofProgram.cs, configure your application:Use HttpClient in Controller or Service:
Inject the
HttpClientwhere you need it (e.g., in a controller or service):This is a basic example, and you might need to adjust it based on the specifics of the OAuth2 provider and API you are working with. Ensure that you handle OAuth2 authentication appropriately, such as obtaining and refreshing access tokens.
Please do let me know if you need additional information
Manish VadukulPosted Jan 14, 2024, 9:22 PM
Hi Sachin
What should i put here ? in Bearer your-access-token and /api/some-endpoint
// Set authorization header for OAuth2 (assuming your API uses OAuth2)
client.DefaultRequestHeaders.Add("Authorization", "Bearer your-access-token");
// Make a request to the API
var response = await client.GetAsync("/api/some-endpoint");
If you are okay than please send me some live test example with link. I don't know how this access Bearer Token to ?
Thanks
Manish VadukulPosted Jan 14, 2024, 7:42 PM
Hi
Thanks for your answer. Which is really helpful.
How can i do consume same Oauth2 rest api using Blazor server side application ?
I want to know where can i set all my config files like base url, client id,secret key in Blazor. And show data in page from Rest API.
regards,
Manish
Sachin SinghPosted Jan 12, 2024, 7:07 PM
Here is the revised example in .NET 6
appsettings.json
Program.cs
API Controller
Manish VadukulPosted Jan 12, 2024, 6:26 PM
Hi Sachin,
This is good example. Could you please give me full example in .NET core 6.0. I can see there is lot of difference in .NET core 3.1 and 6.0.
If you could write for me small .NET core 6.0 Oauth2.0 cusuming API. So it include all programe.cs settings and app settings too. It will be really appreciated.
regards,
Manish
Sachin SinghPosted Jan 12, 2024, 4:21 PM
here is an example
Startup or Main()