Asp.Net Core Web Api and ReactJS: authentication with external login provider without identity

Viewed 2345

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?

1 Answers

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.

Related