【发布时间】:2017-09-28 23:12:45
【问题描述】:
我似乎无法让我的 .net 核心 Web API 与 swashbuckle、OAuth2 和应用程序流一起使用。当我单击 Authorize 按钮时,Fiddler 显示调用正常,并且我的本地 IdentityServer(4) 以 access_token 回复。这一切都很好,但我不认为 Swagger 能解决这个问题,没有任何事情发生,我无法在没有 401 的情况下触发我的控制器方法。我看不到任何 cookie,什么也没有。我确定我错过了一些非常微不足道的东西。有人可以帮我吗?
相关代码:
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");
});
我的 IdentityServer 配置正常。我可以毫无问题地在 Postman 和一个简单的客户端中调用这个 API。我唯一的问题是 Swagger (Swashbuckle.AspNetCore 1.0.0)。
【问题讨论】:
-
使用 Postman 测试 API 时是否传递了 access_token?如果是,如何?我会在此基础上给出解决方案。
-
我愿意,我在标头中传递了令牌(键:授权,值:承载 {token})。
标签: c# .net-core identityserver4 swashbuckle