JWT Authentication and Swagger with .Net core 3.0

后端 未结 6 876
我在风中等你
我在风中等你 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

    If you don't want to add a token manually and you want the scopes to be selectable along with passing a clientId to the identity server you can add something like this.

    I have used implicit flow, but you can configure any flow using the following mechanism:

    options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme()
    {
      Flows = new OpenApiOAuthFlows
      {
        Implicit = new OpenApiOAuthFlow
        {                            
          AuthorizationUrl = new Uri("http://localhost"),
          TokenUrl = new Uri("http://localhost"),
          Scopes = new Dictionary
          {
            { "Foundation API", "FoundationApi" }
          }
        }
      },
      In = ParameterLocation.Header,                    
      Name = "Authorization",
      Type = SecuritySchemeType.OAuth2                    
    });
    

    The output will be like this:

提交回复
热议问题