Is there a clear and simple way to use OAuth login providers in a ReactJS and Asp.Net Core Web API project? I am looking for a solution without Identity and Razor pages. Only ReactJS and Asp.Net Core Web API. And do we need the oidc-client library for this?
-
Did you discovered a solution to your problem? If you find one, could you please include a link to it?Thushara– Thushara2021-06-08 06:41:01 +00:00Commented Jun 8, 2021 at 6:41
-
Have a look at this article - It only shows Google, but it is very detailed and helped me a lot: medium.com/mickeysden/… Github repo: github.com/mickeysden/dotnet-core-react-oauth-exampleJohn Bustos– John Bustos2021-09-09 04:43:37 +00:00Commented Sep 9, 2021 at 4:43
1 Answer
In case you are referring to OpenID Connect:
React app
The react app probably wants to initiate the authentication, get hold of the access token which needs to be included in requests to the API, as well as refresh the access token once it is expired. You don't strictly need a library for that, but it makes your life easier, especially if you use one specific to react, like React OIDC, which is a wrapper for the oidc-client.
An example for including the token in API requests when using axios-hooks can be found in the Authentication and token refresh example. See bearer token for further information.
Web API
To handle the access token provide by the react app, you can add make use of the JWT bearer authentication middleware. Guides for integrating the middleware can be found in Protecting an API using Client Credentials or this question.
For more details you could have a look at the ASP.NET Core - Authentication & Authorization Tutorial.