Click here to Skip to main content
15,445,242 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 22: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[^]
 
Share this answer
 
If you are adding token in "Available authorizations" Pop up/Model. Please make sure you have added a word "Bearer" with space followed by token. This was my issue.
 
Share this answer
 
Comments
CHill60 15-Sep-21 8:59am    
More or less what Sandeep said in Solution 1
Did you find a solution?
I have the same problem trying to implement windows authentication.
 
Share this answer
 
Comments
Richard Deeming 3-Dec-20 5:31am     CRLF
"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