SwaggerUI with .NetCore 3.0 bearer token authorization

后端 未结 4 1677
轮回少年
轮回少年 2021-01-02 23:55

I\'m trying to add authorization header into SwaggerUI api test. below is my Startup.cs

public void ConfigureServices(IServiceCollection services)
    {
              


        
4条回答
  •  误落风尘
    2021-01-03 00:20

    In addition to what Kiril said,in my case I want to get the JWT token through Swagger and this is the code I used, adding the OpenApiSecurityScheme to what he suggested:

    c.AddSecurityDefinition("bearer",
                        new OpenApiSecurityScheme{
                            Flows = new OpenApiOAuthFlows()
                            {
                                ClientCredentials = new OpenApiOAuthFlow()
                                {
                                    TokenUrl = new Uri("https://auth.myauthserver.com/oauth2/token"),
                                    Scopes = new Dictionary(){ {"myscope", "Access API"}},
                                    AuthorizationUrl = new Uri("https://auth.myauthserver.com/oauth2/authorize")
                                }
                            }, 
                            Type = SecuritySchemeType.OAuth2,
                            OpenIdConnectUrl = new Uri("https://myauthserver/.well-known/openid-configuration"),
                            BearerFormat = "JWT",
                            In = ParameterLocation.Header,
                            Scheme = "bearer"
                        });
    

提交回复
热议问题