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
Access-token and refresh-token flow
Sign-in button on the products page
Keycloak-provided login page
Aspire dashboard authentication dialog box
Access token shown in the Aspire host console
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.
Building Distributed Applications that Work ebook for free