【问题标题】:Set Bearer Token with nswag in ASP.NET Core 2.2在 ASP.NET Core 2.2 中使用 nswag 设置不记名令牌
【发布时间】:2020-03-20 04:24:46
【问题描述】:

我有一个 ASP.NET Core 2.2 Web Api,并使用 nswag 添加了 swagger 支持。 Web api 使用生成访问令牌的本地 IdentityServer4 进行保护。

我找到了添加授权按钮和表单并在标题中设置不记名令牌的代码。它有效!

public void ConfigureServices(IServiceCollection services)
{
//...   
            services.AddSwaggerDocument(config =>
            {
                config.DocumentName = "OpenAPI 2";
                config.OperationProcessors.Add(new OperationSecurityScopeProcessor("JWT Token"));
                config.AddSecurity("JWT Token", Enumerable.Empty<string>(),
                    new OpenApiSecurityScheme()
                    {
                        Type = OpenApiSecuritySchemeType.ApiKey,
                        Name = "Authorization",
                        In = OpenApiSecurityApiKeyLocation.Header,
                        Description = "Copy this into the value field: Bearer {token}"
                    }
                );
            });
//...
}

swagger 页面中的按钮

不记名令牌的复制/粘贴表格

我正在寻找一种无需复制/粘贴即可自动执行流程并设置访问令牌的方法。

是否可以设置 nswag 来执行此操作?

【问题讨论】:

    标签: swagger asp.net-core-webapi identityserver4 asp.net-core-2.2 nswag


    【解决方案1】:

    您可以在生成器和 Swagger UI 中启用身份验证。要添加 OAuth2 身份验证 (OpenAPI 3),请在 web api 中:

    services.AddOpenApiDocument(document =>
        {
            document.AddSecurity("bearer", Enumerable.Empty<string>(), new OpenApiSecurityScheme
            {
                Type = OpenApiSecuritySchemeType.OAuth2,
                Description = "My Authentication",
                Flow = OpenApiOAuth2Flow.Implicit,
                Flows = new OpenApiOAuthFlows()
                {
                    Implicit = new OpenApiOAuthFlow()
                    {
                        Scopes = new Dictionary<string, string>
                        {
                            {"api1", "My API"}
    
                        },
                        TokenUrl = "http://localhost:5000/connect/token",
                        AuthorizationUrl = "http://localhost:5000/connect/authorize",
    
                    },
                }
            });
    
            document.OperationProcessors.Add(
                new AspNetCoreOperationSecurityScopeProcessor("bearer"));
        }
    );
    

    配置:

    app.UseOpenApi();
    app.UseSwaggerUi3(settings =>
    {
        settings.OAuth2Client = new OAuth2ClientSettings
        {
            ClientId = "demo_api_swagger",
    
            AppName = "Demo API - Swagger",
    
        };
    });
    

    在身份服务器 4 中,注册 api:

    public static IEnumerable<ApiResource> GetApis()
    {
        return new List<ApiResource>
        {
            new ApiResource("api1", "My API")
        };
    }
    

    还有客户:

    new Client {
        ClientId = "demo_api_swagger",
        ClientName = "Swagger UI for demo_api",
        AllowedGrantTypes = GrantTypes.Implicit,
        AllowAccessTokensViaBrowser = true,
        RedirectUris = {"https://localhost:44304/swagger/oauth2-redirect.html"},
        AllowedScopes = { "api1" }
    },
    

    点击UI中的Authorize按钮后,可以通过IDS4认证并获取api的访问token,然后在发出api请求时token会自动附加到授权请求头中。

    【讨论】:

    • 完美!有用。对于下一个读者:“在我的情况下,我在客户端配置中进行了更改,添加了“RequireConsent = false”,当然还有 che 44304 端口到我的值”
    • 在几个小时内尝试最新的 Swashbuckle.AspNetCore 失败后,我在 DotNet Core 3.1 上使用 NSwag 在几分钟内成功使用了此解决方案。谢谢。
    猜你喜欢
    • 2016-09-25
    • 2020-01-20
    • 1970-01-01
    • 2020-07-09
    • 2022-11-26
    • 2018-10-27
    • 1970-01-01
    • 2019-10-07
    • 2020-07-25
    相关资源
    最近更新 更多