Describe the flow you used, the identity provider, and how the API trusts the tokens. Example for a web app with an ASP.NET Core backend: 1. Register the application with the provider - In Microsoft Entra ID (or Google, Auth0, Okta) register the client application, configure redirect URIs (https://app.example.com/signin-oidc), and create an API registration that exposes scopes such as api://orders/read. - For server-side web apps, store the client secret or (better) a certificate in Key Vault. SPAs and mobile apps are public clients and use PKCE instead of a secret. 2. Choose the right flow - Web apps, SPAs and mobile apps: Authorization Code flow with PKCE. - Background services calling APIs: Client Credentials flow (or managed identity in Azure). - Avoid the implicit and password grants, which are deprecated. 3. Sign-in in the application For a server-rendered or BFF-style app: builder.Services.AddAuthentication(o => { o.DefaultScheme = "Cookies"; o.DefaultChallengeScheme = "oidc"; }) .AddCookie("Cookies") .AddOpenIdConnect("oidc", o => { o.Authority = "https://login.microsoftonline.com/<tenant>/v2.0"; o.ClientId = config["Auth:ClientId"]; o.ClientSecret = config["Auth:ClientSecret"]; o.ResponseType = "code"; o.Scope.Add("api://orders/read"); o.SaveTokens = true; }); For SPAs, MSAL or oidc-client-ts performs the flow in the browser. 4. Protect the API builder.Services.AddAuthentication().AddJwtBearer(o => { o.Authority = "https://login.microsoftonline.com/<tenant>/v2.0"; o.Audience = "api://orders"; }); Then require scopes or roles in policies: p.RequireClaim("scp", "read"). 5. Token handling - Short-lived access tokens with refresh tokens (rotation enabled). - Tokens never logged; HTTPS everywhere; strict redirect URI allow-list; state and nonce parameters validated (the middleware does this). 6. Calling downstream APIs - On-behalf-of flow when the API calls another API as the user, or client credentials when acting as itself. Explain the business reason too — for example "users sign in with their company accounts, and we never handle their passwords."
Ravindra Wadile · · 0 likes