Use JWT (Authorization: Bearer) in Swagger in ASP.NET Core

前端 未结 7 673
闹比i
闹比i 2020-12-02 15:37

I\'m creating a REST api in ASP.NET Core 1.0. I was using Swagger to test but now I added JWT authorization for some routes. (with UseJwtBearerAuthentication)

7条回答
  •  猫巷女王i
    2020-12-02 16:08

    You may add any additional header with API call by using this swagger configuration

    // Register the Swagger generator, defining 1 or more Swagger documents
    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new Info
        {
            Version = "v1",
            Title = "Core API",
            Description = "ASP.NET Core API",
            TermsOfService = "None",
            Contact = new Contact
            {
                Name = "Raj Kumar",
                Email = ""
            },
            License = new License
            {
                Name = "Demo"
            }
        });
        c.AddSecurityDefinition("Bearer", new ApiKeyScheme()
        {
            Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
            Name = "Authorization",
            In = "header",
            Type = "apiKey"
        });
        c.AddSecurityRequirement(new Dictionary>
        {
        {"Bearer",new string[]{}}
        });
    });
    

提交回复
热议问题