A component of ASP.NET for creating RESTful web services that support HTTP-based communication between clients and servers.
In .NET 10 the issue is not with JWT or Swagger itself, but with how OpenAPI/Swagger support is wired and which packages/namespaces are available.
Key points for .NET 10:
-
Microsoft.OpenApi.Modelscomes from Swashbuckle- The
Microsoft.OpenApi.Modelstypes (such asOpenApiSecurityScheme,OpenApiSecurityRequirement, etc.) are provided by the Swashbuckle/OpenAPI libraries, not by ASP.NET Core itself. - In .NET 9 and later, ASP.NET Core added built‑in OpenAPI support and stopped including Swashbuckle by default in new project templates.
- If the project targets .NET 10 and only uses the built‑in OpenAPI pipeline, the
Microsoft.OpenApi.Modelsnamespace will not be available unless Swashbuckle (or another OpenAPI library) is explicitly added.
- The
- Why the code works in .NET 9 but not in .NET 10
- In earlier templates (ASP.NET Core 8 and before), Swashbuckle was commonly included or assumed in samples, so
Microsoft.OpenApi.Modelswas present. - In .NET 9+ the recommended approach is the built‑in OpenAPI support; Swashbuckle is no longer included by default. The same applies when moving to .NET 10.
- As a result, code that directly uses
Microsoft.OpenApi.Modelswill fail to compile in a new .NET 10 project unless Swashbuckle is added manually.
- In earlier templates (ASP.NET Core 8 and before), Swashbuckle was commonly included or assumed in samples, so
- How to fix the compile error
There are two main options:
Option A – Continue using Swashbuckle-style configuration
If the goal is to keep using the AddSwaggerGen pattern exactly as shown:
- Add the Swashbuckle package to the .NET 10 project (for example,
Swashbuckle.AspNetCore). - Ensure the
using Microsoft.OpenApi.Models;directive is present. - Keep the existing
AddSwaggerGenconfiguration, adjusting only for any Swashbuckle v10 changes (for example, delegate-based security requirement configuration as described in the Q&A context).
This restores the Microsoft.OpenApi.Models namespace and allows the JWT security scheme configuration to compile and run as before.
Option B – Use the built‑in OpenAPI support in ASP.NET Core 9+
In .NET 9 and later, ASP.NET Core includes built‑in OpenAPI support that replaces Swashbuckle as the default. The recommended path is:
- Use the built‑in OpenAPI document generation pipeline instead of Swashbuckle for the OpenAPI document.
- If interactive testing via Swagger UI is still desired, follow the guidance in Use the generated OpenAPI documents to host Swagger UI against the built‑in OpenAPI document.
- Configure authentication/authorization at the API level (JWT bearer, policies, etc.) and secure Swagger endpoints as needed using
MapSwagger().RequireAuthorization.
This approach avoids direct dependency on Microsoft.OpenApi.Models and aligns with the new built‑in OpenAPI pipeline.
- Securing Swagger in .NET 10
Regardless of whether Swashbuckle or the built‑in OpenAPI support is used, securing Swagger endpoints in .NET 10 follows the same pattern as in the current docs:
- Call
MapSwagger().RequireAuthorization()to protect the Swagger/OpenAPI endpoints. - Use a valid JWT bearer token when calling the Swagger JSON or UI, for example via Curl:
curl -i -H "Authorization: Bearer {TOKEN}" https://localhost:{PORT}/swagger/v1/swagger.json
- Summary of the cause and solution
- Cause: New .NET 10 project uses the built‑in OpenAPI pipeline and does not include Swashbuckle, so
Microsoft.OpenApi.Modelsis missing. - Solution:
- Either add Swashbuckle back to the project and keep using
Microsoft.OpenApi.ModelsandAddSwaggerGen, or - Migrate to the built‑in OpenAPI support and configure security using the new pipeline and
MapSwagger().RequireAuthorization.
- Either add Swashbuckle back to the project and keep using
Once Swashbuckle is explicitly referenced (Option A) or the configuration is updated to the built‑in OpenAPI model (Option B), the compile errors disappear and JWT‑protected endpoints can again be tested via Swagger UI.
References:
- ASP.NET Core web API documentation with Swagger / OpenAPI
- Configure JWT bearer authentication in ASP.NET Core
- Breaking changes in ASP.NET Core 10
- Deprecation of WithOpenApi extension method
- Microsoft.Extensions.ApiDescription.Client package deprecated
- Authorization Issue with AddSecurityRequirement for OAuth2 in .NET 10 (C# 14) Swagger Configuration - Microsoft Q&A