![](/img/trans.png)
[英]How to implement a client_credentials grant type in an angular http request?
[英]How do I configure SwaggerGen with OpenIddict parameters for client credentials grant?
我试图弄清楚如何配置 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()
中配置什么来进行配置,这样我们就可以通过 Swagger 生成的交互式文档轻松测试我们的 API。
在定义OpenApiSecurityScheme
时,您需要指定更多选项。
您可以通过以下方式进行设置:
TokenUrl
。 客户端凭据流适用于/token
端点,因此我们必须为其提供正确的 URL。 这里我使用了 IdentityServer 的演示服务器Bearer
方案的Authorization
标头中发送。public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(
c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "ApiPlayground", Version = "v1" });
c.AddSecurityDefinition(
"oauth",
new OpenApiSecurityScheme
{
Flows = new OpenApiOAuthFlows
{
ClientCredentials = new OpenApiOAuthFlow
{
Scopes = new Dictionary<string, string>
{
["api"] = "api scope description"
},
TokenUrl = new Uri("https://demo.identityserver.io/connect/token"),
},
},
In = ParameterLocation.Header,
Name = HeaderNames.Authorization,
Type = SecuritySchemeType.OAuth2
}
);
c.AddSecurityRequirement(
new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{ Type = ReferenceType.SecurityScheme, Id = "oauth" },
},
new[] { "api" }
}
}
);
}
);
}
这是所有设置后的外观:
身份验证后,它会填充令牌:
现在我们可以发送请求,Swagger UI 如我们所期望的那样在标头中包含令牌:
最后,我们可以使用一些默认值预填充 auth 对话框:
在我们设置 Swagger UI 的Startup:Configure
方法中,我们可以指定客户端 id + secret(这违背了目的,但在本地开发中可能会被证明是有用的)
app.UseSwaggerUI(c => {
c.SwaggerEndpoint("/swagger/v1/swagger.json", "ApiPlayground v1");
c.OAuthClientId("m2m");
c.OAuthClientSecret("secret");
});
您需要配置 swagger 来发现 OpenIddict 配置。 请参阅下面的代码示例:
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 = HeaderNames.Authorization,
Flows = new OpenApiOAuthFlows
{
ClientCredentials = new OpenApiOAuthFlow
{
AuthorizationUrl = new Uri("/connect/token", UriKind.Relative),
TokenUrl = new Uri("/connect/token", UriKind.Relative)
}
},
OpenIdConnectUrl = new Uri("/.well-known/openid-configuration", UriKind.Relative)
});
options.AddSecurityRequirement(
new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{ Type = ReferenceType.SecurityScheme, Id = "oauth" },
},
Array.Empty<string>()
}
}
);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.