JWT Authentication and Swagger with .Net core 3.0

后端 未结 6 737
我在风中等你
我在风中等你 2020-12-25 12:31

I am developing some Web Api with .Net core 3.0 and want to integrate it with SwashBuckle.Swagger. It is working fine, but when I add JWT authentication, it does not work as

6条回答
  •  醉梦人生
    2020-12-25 13:12

    In the accepted answer "Bearer " is required to be written before the actual token. A similar approach in which typing "Bearer " can be skipped is the following:

    c.SwaggerDoc("v1", new OpenApiInfo { Title = "Example API", Version = "v1" });
    
    c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
    {
        Type = SecuritySchemeType.Http,
        BearerFormat = "JWT",
        In = ParameterLocation.Header,
        Scheme = "bearer",
        Description = "Please insert JWT token into field"
    });
    
    c.AddSecurityRequirement(new OpenApiSecurityRequirement
    {
        {
            new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference
                {
                    Type = ReferenceType.SecurityScheme,
                    Id = "Bearer"
                }
            },
            new string[] { }
        }
    });
    

    Here only pasting the JWT token is required for this to work.

提交回复
热议问题