Nicefsf
Nicefsf

Reputation: 140

How to authenticate in asp.net core rest api from react app

I have 2 asp.net core web applications: REST API and React UI. React UI uses default Individual User Accounts option for authenticating. They are separate solutions.
What I want is to authenticate in API using this default authentication. But I'm don't know how can I do this. So what I want is
Make a call from UI -> Grab user credentials -> Go to API method -> Validate user (e.g. role) -> Return response As far as I know, default authentication sets AspNetCore.Identity.Application cookie that used for auth in react. Probably, I can somehow parse it or just use it to authenticate on API side. I thought it's JWT token, but seems like it's not

Upvotes: 1

Views: 1067

Answers (1)

Mellet
Mellet

Reputation: 1306

React application sends request:

  • Url: /auth
  • Body: JSON.stringify({username: "john", password: "password123" })

REST API handles /auth request

  • Validate that username exists
  • Checks that password is correct
  • Either sets a cookie that the user is signed in or returns a token that the React app can save for future requests

React app sends request to get items

  • Url: /items
  • Headers, include token or set withCredentials to pass cookie

REST API handles /items request

  • Makes sure cookie/token is set
  • Validate token/cookie
  • Return items if everything is ok.

Upvotes: 1

Related Questions