Click here to Skip to main content
14,771,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I add swagger to my project and use authorization. It works properly with Postman but when want to use with Swagger get 401 Undocumented

What I have tried:

public void ConfigureServices(IServiceCollection services)
{
   ...
    services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "APIs", Version = "v1" });
                
                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    In = ParameterLocation.Header,
                    Name = "Authorization",
                    Type = SecuritySchemeType.ApiKey,
                });

                c.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                     {
                        new OpenApiSecurityScheme
                        {
                            Name = "Token",
                            Type = SecuritySchemeType.ApiKey,
                            In = ParameterLocation.Header,
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id = "Token",
                            },
                        },
                        Array.Empty<string>()
                     }
                });
            });
           ...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IUnitOfWork uow)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            //########################################## Swagger
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "APIs");
            });
            //##########################################

            app.UseHttpsRedirection();

            app.UseRouting();
            app.UseAuthentication();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
            ....
}
Posted
Updated 2-Dec-20 23:32pm

I don't see where are you setting the bearer token in your code. You define there would be one though.

Would suggest you to compare the network call when of your request when sent through swagger and via postman and you should observe that swagger call does not have bearer token value.

Reference on how to use authorization in swagger: ASP.NET Core Swagger UI Authorization using IdentityServer4 - Scott Brady[^]
   
Did you find a solution?
I have the same problem trying to implement windows authentication.
   
Comments
Richard Deeming 3-Dec-20 5:31am
   
"Me too" is not a solution to the question.

If you want to ask for more information, click the "Have a Question or Comment?" button under the question and post a comment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900