For a single-page application, the recommended pattern is the Authorization Code flow with PKCE, combined with an API that validates access tokens — or, even better, a Backend-for-Frontend (BFF). Roles - Identity provider (Microsoft Entra ID, Auth0, Keycloak, Duende IdentityServer) authenticates the user. - OpenID Connect adds identity on top of OAuth 2.0: the ID token tells the SPA who signed in. - OAuth 2.0 access tokens authorize calls to the API. Flow with PKCE 1. React redirects to the identity provider with a code_challenge. 2. The user signs in; the provider redirects back with an authorization code. 3. The SPA exchanges code + code_verifier for tokens. 4. Every API call sends Authorization: Bearer <access token>. Do not use the implicit flow; it is deprecated. API side (ASP.NET Core) builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(o => { o.Authority = "https://login.example.com/"; o.Audience = "orders-api"; }); The handler validates signature, issuer, audience and expiry. Then apply [Authorize] and policies based on scopes, roles or claims, plus resource-level checks. Where to keep tokens - Tokens in localStorage can be stolen by XSS. Prefer in-memory storage with short-lived access tokens and refresh token rotation. - The BFF pattern is safer: an ASP.NET Core backend performs the OIDC flow as a confidential client and keeps tokens server-side; the browser only holds a Secure, HttpOnly, SameSite cookie. The BFF proxies API calls and adds tokens. You then add CSRF protection for cookie-based calls. Other essentials - Strict redirect URI allow-lists and CORS configured only for your front-end origin. - Least-privilege scopes; separate audiences per API. - Short access-token lifetimes (5–15 minutes) and revocation for refresh tokens. - Content-Security-Policy and sanitisation to reduce XSS risk. - Validate authorization on the server for every request — hiding buttons in React is only UX.
Ravindra Wadile · · 0 likes