c# - Swashbuckle.AspNetCore v1.0.0,带 OAuth2,流程 : application -> IdentityServer4

标签 c# .net-core identityserver4 swashbuckle

我似乎无法让我的 .net core Web API 与 swashbuckle、OAuth2 和应用程序流程一起工作。当我单击“授权”按钮时,Fiddler 显示调用正常,并且我的本地 IdentityServer(4) 回复了 access_token。这一切都很棒,但我不认为 Swagger 会注意到这一点,什么也没有发生,而且我无法在没有收到 401 的情况下触发我的 Controller 方法。我没有看到 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)。

最佳答案

我们当前的项目有一个非常相似的设置。我们的其余 API 通过 jwt 承载身份验证和 azure ad b2c 进行保护。在这种情况下,无法自动获取 token 。

这个解决方案非常适合我们:https://stackoverflow.com/a/39759152/536196

services.AddSwaggerGen(c =>
{
    c.OperationFilter<AuthorizationHeaderParameterOperationFilter>();
});

之后,当您运行 swagger UI 时,您应该会看到 token 的附加字段。

关于c# - Swashbuckle.AspNetCore v1.0.0,带 OAuth2,流程 : application -> IdentityServer4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43720776/

相关文章:

c# - 如何将 `where T : U` 泛型类型参数约束从 C# 转换为 F#?

c# - 使用 C# 中的串行端口从地磅机获取重量

c# - 接口(interface)作为一个用于继承的原因,而不是实现者

oauth-2.0 - 客户端凭据流的访问 token 中的 invalid_scope 错误

testing - 如何实现 IdentityServer4 的集成测试?

identityserver4 - 部署到 Azure 导致 fatal error

C# - 动态创建MenuStrip

c# - 如何在引用外部库的代码中提供注释

api - 在Docker上托管后如何在.Net Core Web API中创建和访问文件夹结构

asp.net - 从枚举 .NET Core 中选择列表