Share via

.NET 10 issues with OpenApi

Thomas Borby Lancestremère 0 Reputation points
2026-03-27T10:08:10.6866667+00:00

Hello

I created an ASP.NET Core Web Api project with the version of .NET 10 , to utilize Swagger with authentication

I created the project to test my API endpoints with authentication and out of the blue I found this stange issue, where I couldn't utilized authorize with the generated token?

Usually this code should work for just that purpose:

builder.Services.AddSwaggerGen(setup =>
{
    setup.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
    {
        Title = "API test project",
        Version = "v1",
        Description = "API with JWT authentication – use Authorize button to insert token"
    });

    // JWT Bearer definition
    var jwtSecurityScheme = new Microsoft.OpenApi.Models.OpenApiSecurityScheme
    {
        Scheme = "bearer",
        BearerFormat = "JWT",
        Name = "Authorization",
        In = Microsoft.OpenApi.Models.ParameterLocation.Header,
        Type = Microsoft.OpenApi.Models.SecuritySchemeType.Http,
        Description = "Insert your JWT token here with 'Bearer ' in front",
        Reference = new Microsoft.OpenApi.Models.OpenApiReference
        {
            Id = JwtBearerDefaults.AuthenticationScheme,
            Type = Microsoft.OpenApi.Models.ReferenceType.SecurityScheme
        }
    };

    setup.AddSecurityDefinition(jwtSecurityScheme.Reference.Id, jwtSecurityScheme);

    setup.AddSecurityRequirement(new Microsoft.OpenApi.Models.OpenApiSecurityRequirement
    {
        { jwtSecurityScheme, Array.Empty<string>() }
    });
});

It works in .NET 9, but somehow not in .NET 10?

I get errors about "The type or namespace name 'Models' does not exist in the namespace 'Microsoft.OpenApi'", where Models is missing from the OpenApi package?

I tried to included it by "using Microsoft.OpenApi.Models" in the namespace, but it only gives errors?

I don't know if it's a bug in the .NET 10 or if you have just changed the way to utilize OpenApi?

I've searched and asked people and chatbots, but with no solution...

If you know the cause to this issue, I'd apreciate it if you could text me back with the followed solution :-)

Best regards

Thomas

Developer technologies | ASP.NET | ASP.NET API
{count} votes

1 answer

Sort by: Most helpful
  1. Q&A Assist
    2026-03-27T10:08:37.9466667+00:00
    AI generated content. This question contains an answer created with AI from Q&A; Assist. Learn more

    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:

    1. Microsoft.OpenApi.Models comes from Swashbuckle
      • The Microsoft.OpenApi.Models types (such as OpenApiSecurityScheme, 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.Models namespace will not be available unless Swashbuckle (or another OpenAPI library) is explicitly added.
    2. 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.Models was 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.Models will fail to compile in a new .NET 10 project unless Swashbuckle is added manually.
    3. 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:

    1. Add the Swashbuckle package to the .NET 10 project (for example, Swashbuckle.AspNetCore).
    2. Ensure the using Microsoft.OpenApi.Models; directive is present.
    3. Keep the existing AddSwaggerGen configuration, 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:

    1. Use the built‑in OpenAPI document generation pipeline instead of Swashbuckle for the OpenAPI document.
    2. 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.
    3. 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.

    1. 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
    
    1. 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.Models is missing.
    • Solution:
      • Either add Swashbuckle back to the project and keep using Microsoft.OpenApi.Models and AddSwaggerGen, or
      • Migrate to the built‑in OpenAPI support and configure security using the new pipeline and MapSwagger().RequireAuthorization.

    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:

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.