ARTICLE AD BOX
I want to not get token from login endpoint in Swagger every time I run and debug the API code. I have added this code to the program.cs file to get authenticated, but when I test an endpoint with an [Authorize] attribute, I get 401 response. How to fix this?
builder.Services.AddSwaggerGen(opt => { opt.SwaggerDoc("v1", new OpenApiInfo { Title = "•♣• My Api •♣•", Version = "Version: 1.1.1" }); opt.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { In = ParameterLocation.Header, Description = "Please enter token", Name = "Authorization", Type = SecuritySchemeType.Http, BearerFormat = "JWT", Scheme = "bearer" }); opt.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type=ReferenceType.SecurityScheme, Id="Bearer" } }, new string[]{} } }); }); builder.Host.UseSerilog((context, configuration) => configuration.ReadFrom.Configuration(context.Configuration)); var app = builder.Build(); app.UseSerilogRequestLogging(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(c => { c.ConfigObject.AdditionalItems = new Dictionary<string, object> { { "requestInterceptor", "function (req) { req.headers['Authorization'] = 'Bearer someTokenThatIsValid'; return req; }" } }; }); }1
