Hi @Tirth Shah ,
Thanks for sharing the details!
I see two things happening after your migration from .NET 8 to .NET 10:
- The Swagger configuration fails to compile because
AddSecurityRequirementnow expects a delegate (Func<OpenApiDocument, OpenApiSecurityRequirement>). - Even after updating,
[Authorize]endpoints return 401 Unauthorized, which usually points to middleware order or JWT settings rather than Swagger itself.
I recommend you check these out first:
1. Update Swagger configuration for .NET 10 and Swashbuckle v10
Swashbuckle changed how security requirements are added. Define the HTTP Bearer scheme and use a delegate for the requirement:
using Microsoft.OpenApi.Models;
const string schemeId = "bearer";
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "CMS API", Version = "v1" });
options.AddSecurityDefinition(schemeId, new OpenApiSecurityScheme
{
Type = SecuritySchemeType.Http,
Scheme = "bearer", // lowercase per RFC 7235
BearerFormat = "JWT",
Description = "JWT Authorization header using Bearer scheme"
});
options.AddSecurityRequirement(document =>
{
var requirement = new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = schemeId
}
},
Array.Empty<string>() // no scopes for JWT
}
};
return requirement;
});
});
You can check Swashbuckle migration guide and OpenApiSecurityRequirement Class for better clarity.
2. Check middleware order and JWT settings
Make sure you call:
app.UseSwagger();
app.UseSwaggerUI();
app.UseAuthentication(); // BEFORE endpoints
app.UseAuthorization();
// Then map endpoints
app.MapControllers(); // or MapGroup/MapGet...
before mapping endpoints. Then confirm your AddJwtBearer options match the token’s Issuer, Audience, and SigningKey. Full guidance here: Configure JWT bearer authentication in ASP.NET Core
3. Use Swagger UI correctly
Click Authorize, paste only the raw JWT (do not include Bearer, Swagger adds it automatically). Verify in the “Request” panel that the header shows Authorization: Bearer <token>.
I also found this post that tackled similar issue, maybe you could check it out when you have the time:
Disclaimer: Some of these links are non-Microsoft website. The pages appear to be providing accurate, safe information. Watch out for ads on the site that may advertise products frequently classifies as a PUP (Potentially Unwanted Products). Thoroughly research any product advertised on the site before you decide to download and install it.
I hope this is helpful to you! If you have any more questions, please feel free to reach out. I'll be happy to help out!