我似乎无法使我的.NET Core Web API与SwashBuckle,OAuth2一起使用应用程序流。当我单击"授权"按钮时,提琴手显示通话还可以,我的本地身份服务器(4(用access_token回复。这一切都很棒,但我不认为Swagger将其捡起来,没有发生任何事情,而且我无法触发我的控制器方法,而没有得到401。我敢肯定我错过了一些超级琐碎的东西。有人可以帮我吗?
相关代码:
在startup.cs
中配置服务c.AddSecurityDefinition("oauth2", new OAuth2Scheme
{
Type = "oauth2",
Flow = "application",
TokenUrl = "http://localhost:61798/connect/token",
Scopes = new Dictionary<string, string>
{
{ "readAccess", "Access read operations" },
{ "writeAccess", "Access write operations" }
}
});
在startup.cs
中配置app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
Authority = "http://localhost:61798",
RequireHttpsMetadata = false,
ApiName = "api1",
AutomaticAuthenticate = true, //Doesn't change anything...
});
....
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "V1 Docs");
c.ConfigureOAuth2("Swagger", "swaggersecret", "swaggerrealm", "Swagger UI");
});
我的身份服务器已配置好。我可以在Postman中称呼此API,而无需任何问题。我唯一的问题是Swagger(swashbuckle.aspnetcore 1.0.0(。
我们对当前项目有非常相似的设置。我们的REST API具有JWT携带者身份验证和Azure AD B2C的确保。在这种情况下,没有办法自动拾取令牌。
此解决方案非常适合我们:https://stackoverflow.com/a/39759152/536196
services.AddSwaggerGen(c =>
{
c.OperationFilter<AuthorizationHeaderParameterOperationFilter>();
});
之后,当您运行Swagger UI时,您应该看到一个代币的其他字段。