This repo contains a sample app for hosting a React client from an ASP.NET Core WebAPI. During Development, requests for the client are forwarded to the vite development server, making the client application appear as if it was hosted through the server.
This enables the use of unified security headers in development and production, and allows us to use cookie authentication, as you'd use with a Razor/MVC or Blazor Server app.
- AAD Config:
ServiceCollectionExtension.cs
~L43ff. Configures cookie auth
- Dev Server Proxy:
Program.cs
~L106ff_Host.cshtml
- Security Headers + CSP with Nonce:
SecurityHeadersDefinitions.cs
- Reading the Nonce in Client app:
main.tsx
- CSP Workaround for Swagger:
SwaggerCspRelaxingHeaderService.cs
- XSRF/CSRF Mitigation with Synchronizer Pattern:
_Host.cshtml
- create tokens and pass then through cookies (one HTML-only, one not)HttpClient.ts
- read request token from non-HTML-only cookie, add to header
- Swagger with CSRF:
ServiceCollectionExtension.cs
~L65ffSwaggerHeaderFilter.cs
- automatically add antiforgery request token (check swagger, it's shown as a form field)
- PWA:
vite.config.ts
- contains Vite-PWA configservice-worker.js
- basic offline support
- Open the solution and launch the server app in Kestrel.
- Run the client app at /src/Client by running
npm install
thennpm run dev
- Access the application at
localhost:5001
- Build the client app at /src/Client by running
npm install
thennpm build
. The build output is placed in the server project'swwwroot
folder - Build the solution, making sure the
wwwroot
folder is included in the build - Run the webAPI through Kestrel and access it via
https