reactjsasp.net-coreauthenticationasp.net-core-3.1.net-7.0

Use React Authentication with .NET backend?


We have a project using a React frontend with a .NET Core backend. We're in the process of implementing Ping One for authentication. We have a working version of the auth, using the SDK located here.

Now we need to have that user also be authenticated on the .NET Core side. Specifically, we need to get an IPrincipal from HttpContext, which I expect would be a ClaimsPrincipal with appropriate information set, and even more specifically, the IPrincipal.Identity.Name field mapped to the authenticating claims identity identifier and the IPrincipal.Identity.IsAuthenticated field set to true.

I am, however, at an absolute loss as to how to make this happen. The authentication context does provide an access_token and an id_token. Is there some way to call something within IPrincipal to set the authentication session state by passing an access_token or id_token? I do see from research that there does seem to be some concept using JwtBearerToken - such as for example, this tutorial. Is that the right approach to be following? And, if it is, how would we pass that information from the client to the server?

As another approach, I saw https://stackoverflow.com/a/44625464/1289046 here on SO, as a potential way of setting the IPrincipal directly. Could this approach work? And if so, are there any potential drawbacks I need to be aware of?

Please let me know if there's any other info I can provide, as this is an area I'm not hugely familiar with - in the past we've used pure server-side authentication by simply calling Authorize, which does all the work for us.


Solution

  • Just posting the comment @Peter B mentioned as the answer - we went ahead and changed the auth flow to use the backend for auth, and the frontend just automatically recognizes being authenticated as it is using the same principal.