我正在尝试弄清楚如何配置SwaggerGen来填充/显示OpenIddict和客户端凭据授予的字段/参数。
services.AddDbContext<AppDbContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
options.UseOpenIddict();
});
services.AddOpenIddict()
.AddCore(options =>
{
// Configure OpenIddict to use the Entity Framework Core stores and models.
// Note: call ReplaceDefaultEntities() to replace the default entities.
options.UseEntityFrameworkCore().UseDbContext<AppDbContext>();
})
.AddServer(options =>
{
// Enable the token endpoint.
options.SetTokenEndpointUris("/connect/token");
// Enable the client credentials flow.
options.AllowClientCredentialsFlow();
// Register the signing and encryption credentials.
options.AddDevelopmentEncryptionCertificate()
.AddDevelopmentSigningCertificate();
// Register the ASP.NET Core host and configure the ASP.NET Core options.
options.UseAspNetCore()
.EnableTokenEndpointPassthrough();
})
.AddValidation(options =>
{
// Import the configuration from the local OpenIddict server instance.
options.UseLocalServer();
// Register the ASP.NET Core host.
options.UseAspNetCore();
});
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "PCM", Version = "v1" });
options.AddSecurityDefinition("Authentication", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OpenIdConnect,
Description = "Description",
In = ParameterLocation.Header,
Name = "Notsure",
Flows = new OpenApiOAuthFlows
{
ClientCredentials = new OpenApiOAuthFlow
{
AuthorizationUrl = new Uri("/connect/token", UriKind.Relative),
TokenUrl = new Uri("/connect/token", UriKind.Relative),
Scopes = new Dictionary<string, string>()
{
}
}
},
OpenIdConnectUrl = new Uri("/connect/authorize", UriKind.Relative)
});
});
它显示了“授权”按钮,但当我单击它时,它会打开一个空模态,如下图所示:
感谢任何人谁可以告诉我一些文档,将解释我需要配置在services.AddSwaggerGen()
得到这个配置,使我们可以很容易地测试我们的API通过交互式文档生成的Swagger。
2条答案
按热度按时间3bygqnnd1#
在定义
OpenApiSecurityScheme
时,您需要指定多个选项。以下是您可以进行设置的方法:
TokenUrl
。客户端凭据流在/token
端点上工作,因此我们必须为其给予一个正确的URL。Bearer
方案在Authorization
标头中发送它。以下是设置完成后的外观:
进行身份验证后,它将被令牌填充:
现在我们可以发送请求了,Swagger UI如我们所期望的那样在头中包含了令牌:
预填充身份验证弹出窗口
最后,我们可以在auth对话框中预填充一些默认值:
在设置Swagger UI的
Startup:Configure
方法中,我们可以指定客户端id + secret(这与目的不符,但在本地开发中可能会证明有用)参考
1tu0hz3e2#
您需要配置swagger来发现OpenIddict配置。请参见下面的代码示例: