JWT Authorization ICON in .NET 10 Issue

Marjan 20 Reputation points
2025-12-15T09:30:53.0633333+00:00

(Moved from: Community Center | Discuss the Q&A site | Site suggestion)

I'm implementing JWT authorization in .NET 10, but I noticed that the [Authorize] attribute and the lock icon are missing. Has anyone encountered this issue, or does anyone have a solution?

// JWT settings from configuration
 IConfigurationSection? jwtSettings = builder.Configuration.GetSection("JwtSettings");
 // Ensure all required JWT settings are present
 string? secretKey = jwtSettings["SecretKey"] ?? throw new InvalidOperationException("JWT SecretKey is missing.");
 string? issuer = jwtSettings["Issuer"] ?? throw new InvalidOperationException("JWT Issuer is missing.");
 string? audience = jwtSettings["Audience"] ?? throw new InvalidOperationException("JWT Audience is missing.");
 builder.Services.AddAuthentication(options =>
 {
 
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;

 options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
 })
 .AddJwtBearer(options =>
 {
 options.TokenValidationParameters = new TokenValidationParameters

 {

     ValidateIssuer = true,

     ValidateAudience = true,

     ValidateLifetime = true,

     ValidateIssuerSigningKey = true,

     ValidIssuer = issuer,

     ValidAudience = audience,

     IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey))

 };

 });
 builder.Services.AddSwaggerGen(c =>
 {
 c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });

 c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme

 {

     Description = "Enter 'Bearer {token}'",

     Name = "Authorization",

     In = ParameterLocation.Header,

     Type = SecuritySchemeType.ApiKey,

     Scheme = JwtBearerDefaults.AuthenticationScheme

 });

 c.AddSecurityRequirement(document => new OpenApiSecurityRequirement

 {

     [new OpenApiSecuritySchemeReference(JwtBearerDefaults.AuthenticationScheme, document)] = []

 });

 });
 builder.Services.AddAuthorization();
 var app = builder.Build();
 // Configure the HTTP request pipeline.
 if (app.Environment.IsDevelopment())
 {
 app.MapOpenApi();

 // Enable Swagger UI

 app.UseSwaggerUI(options =>

 {

     options.SwaggerEndpoint("/openapi/v1.json", "OX API v1");

     options.RoutePrefix = "swagger";

 });
 }
 app.UseGetIPAddress(); 
 app.UseHttpsRedirection();
 app.UseAuthentication(); 
 app.UseAuthorization();
Developer technologies | .NET | Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Q&A Assist
    2025-12-15T09:31:05.8566667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    It seems you are facing an issue with the [Authorize] attribute and the lock icon not appearing in your .NET 10 application while implementing JWT authorization. Here are a few steps you can take to troubleshoot and potentially resolve the issue:

    1. Ensure Proper Configuration: Double-check your JWT settings in the configuration file to ensure that the SecretKey, Issuer, and Audience are correctly set and being read by your application.
    2. Add Authorization Services: Make sure you have added the authorization services in your Startup.cs or Program.cs file using builder.Services.AddAuthorization(); which you have done correctly.
    3. Check Swagger Configuration: In your Swagger configuration, ensure that the security definition and requirements are set up correctly. It looks like you are using AddSecurityDefinition and AddSecurityRequirement, which is good. Make sure the Scheme matches the one you have set in the authentication options.
    4. Use the [Authorize] Attribute: Ensure that you are applying the [Authorize] attribute to your controllers or actions correctly. For example:
         [Authorize]
         public class MyController : ControllerBase
         {
             // Your actions here
         }
      
    5. Check Middleware Order: The order of middleware in the pipeline is crucial. Ensure that app.UseAuthentication(); is called before app.UseAuthorization(); in your Configure method.
    6. Development Environment: If you are in a development environment, ensure that Swagger UI is correctly set up to display the authorization options. You can check the Swagger UI to see if the Bearer token input field appears.

    If you have checked all these points and the issue persists, consider looking into the console or logs for any errors that might give more insight into what is going wrong.


    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.