Overview

8 Application security in Aspire

This chapter explains how to secure an Aspire-orchestrated distributed application end to end, extending earlier introductory material into a full implementation of authentication and authorization. It emphasizes that public web applications should restrict access to authorized users, and that the same security model should protect both the user interface and the backend API. The discussion also applies these ideas to the Aspire dashboard itself, which needs its own protection so that only trusted users can view logs and manage services.

The chapter uses single sign-on as the core example and describes how OpenID Connect handles authentication while OAuth supports authorization. It walks through the basic SSO flow: a user attempts to access a protected resource, is redirected to an identity provider, logs in, and returns with a token that confirms identity and can be reused across protected applications. In the Aspire setup, the app must know the identity provider’s address, the provider must know each application’s redirect URL, and local development uses service-discovery-style configuration to pass these values into the relevant containers and apps.

The implementation then secures the web frontend by configuring cookie authentication, OIDC challenge handling, token storage, and middleware, while marking protected pages and selectively hiding order actions from anonymous users with authorization-aware UI components. It also adds sign-in and sign-out endpoints and ensures the Blazor app carries authentication state properly. On the API side, it switches to JWT bearer authentication, validates incoming tokens, protects order-placement endpoints, and forwards the user’s access token from the frontend when calling the API. Finally, it shows how to secure the Aspire dashboard through host configuration, including the default browser-token approach and alternative authentication modes, while warning that dashboard login should not depend on an IdP hosted inside the same Aspire deployment.

SSO flow
figure
Access-token and refresh-token flow
figure
Sign-in button on the products page
figure
Keycloak-provided login page
figure
Aspire dashboard authentication dialog box
figure
Access token shown in the Aspire host console
figure

Summary

  • Authentication is the mechanism that requires users and other clients to prove that they are who they claim to be.
  • Authorization is the mechanism that ensures that authenticated clients have the required permission to access the requested resource.
  • SSO security is managed by a shared IdP.
  • OIDC is a commonly used authentication mechanism. OAuth is an authorization mechanism that can be used alongside OIDC.
  • An authenticated UI application can pass credentials and the user’s permission information to a backend API via a JWT.
  • We can use Aspire’s service-discovery feature to secure our hosted applications by using an internally hosted IdP.
  • The Aspire dashboard has its own security configuration and several options for securing it. The default security option for the Aspire dashboard is a URL token.

FAQ

What is the main security goal when building distributed applications in Aspire?Ensuring that only authorized users can access the UI, the backend API, and the Aspire dashboard. The chapter emphasizes end-to-end authentication and authorization so security applies across all parts of the application.
What is single sign-on (SSO) in the context of this chapter?SSO lets a user authenticate once and then access multiple applications in the same ecosystem without logging in again each time, as long as they trust the same identity provider.
What is the difference between authentication and authorization?Authentication verifies who the user is, usually by checking credentials. Authorization determines what that authenticated user is allowed to access, such as pages or actions reserved for admins or logged-in users only.
Why does the chapter use OpenID Connect and OAuth together?OpenID Connect handles authentication, while OAuth handles authorization. Together, they provide the standard protocol stack used for secure SSO and access control in Aspire applications.
How does the web frontend know where to send users for login?The web frontend is configured with the identity provider’s authority URL and a client setup that allows it to redirect unauthenticated users to the IdP login page automatically.
Why are service-discovery URLs not used directly as OIDC authority URLs?Because Aspire service-discovery addresses are not standard browser-facing HTTP URLs. The chapter instead builds real public URLs and passes them to the identity provider and authentication configuration.
How is authentication configured in the UI application?The UI app uses cookie authentication plus OpenID Connect. Cookies store the authenticated session locally, while OIDC is used to challenge unauthenticated users and log them in through the identity provider.
How is the API protected from unauthorized requests?The API uses JWT bearer authentication and applies RequireAuthorization() to protected endpoints. Clients must send a valid JWT in the Authorization header to access secured API routes.
How does the frontend send the JWT to the API?The frontend reads the access token from the authenticated user’s claims and adds it to outgoing API requests as a Bearer token in the Authorization header.
How can the Aspire dashboard be secured?The dashboard can be configured in the host app’s appsettings.json using the Dashboard:Frontend:AuthMode setting. The chapter mentions modes such as BrowserToken, OpenIdConnect, and Unsecured, with a warning not to use unsecured mode outside development.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Distributed Applications that Work Building Distributed Applications that Work ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Distributed Applications that Work Building Distributed Applications that Work ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Distributed Applications that Work Building Distributed Applications that Work ebook for free