【问题标题】:Bearer token not getting included in SwaggerUI不记名令牌未包含在 SwaggerUI 中
【发布时间】:2020-12-29 17:52:22
【问题描述】:

我正在使用 AspNet Boilerplate 框架。我正在尝试在我的应用程序中通过 AzureAD 实现身份验证。我正在使用 ASP.NET core 3.0 并通过 swagger 测试我的应用程序。我看到令牌未包含在标头中。

Startup.cs 的ConfigureServices() 方法

                {
                    Type = SecuritySchemeType.OAuth2,
                    Flows = new OpenApiOAuthFlows()
                    {
                        Implicit = new OpenApiOAuthFlow()
                        {
                            TokenUrl = new Uri(My Token Url),
                            AuthorizationUrl = new Uri(My Authorization Url),
                            Scopes = { { "api://357...../user_impersonation", "Access adt-service" } }
                        }
                    }
                });

Startup.cs 的Configure() 方法:

            {                
                options.OAuthClientId("22............");
                options.OAuthScopeSeparator(" ");
                
            });

如果我遗漏了什么,请告诉我。

【问题讨论】:

    标签: asp.net-core swagger-ui aspnetboilerplate


    【解决方案1】:

    当您进行身份验证时,您是否首先收到了令牌?在我的回答中,我假设你是。

    您没有完全在您的 sn-p 中显示这一点,但您必须执行以下类似操作来配置 Swagger 服务,对吧?

    services.AddSwaggerGen(options =>
    {
        options.SwaggerDoc(...);
    
        options.AddSecurityDefinition(
            "oauth",
            new OpenApiSecurityScheme
            {
                Type = SecuritySchemeType.OAuth2,
                Flows = new OpenApiOAuthFlows()
                {
                    Implicit = new OpenApiOAuthFlow()
                    {
                        TokenUrl = new Uri(My Token Url),
                        AuthorizationUrl = new Uri(My Authorization Url),
                        Scopes = { { "api://357...../user_impersonation", "Access adt-service" } }
                    }
                }
        });
    });
    

    如果您是,只需在您的选项中添加安全要求,令牌应该已经包含在您的请求中:

    services.AddSwaggerGen(options =>
    {
        options.SwaggerDoc(...);
    
        options.AddSecurityDefinition(
            "oauth",
            new OpenApiSecurityScheme
            {
                Type = SecuritySchemeType.OAuth2,
                Flows = new OpenApiOAuthFlows()
                {
                    Implicit = new OpenApiOAuthFlow()
                    {
                        TokenUrl = new Uri(My Token Url),
                        AuthorizationUrl = new Uri(My Authorization Url),
                        Scopes = { { "api://357...../user_impersonation", "Access adt-service" } }
                    }
                }
        });
    
        options.AddSecurityRequirement(
            new OpenApiSecurityRequirement {
            {
                new OpenApiSecurityScheme
                {
                    Reference = new OpenApiReference {
                        Type = ReferenceType.SecurityScheme,
                        Id = "oauth"
                    }
                },
                oauthScopes.Keys.ToArray() // array with scopes' keys used above in the security definition
            }
        });
    });
    

    请注意添加options.AddSecurityRequirement。名称/id "oauth" 只是安全定义的标识名称。

    这些 sn-ps 适用于 .net 5。我相信它们可能适用于 .net core 3

    【讨论】:

    • 这一行是关键! oauthScopes.Keys.ToArray() // 具有上述安全定义中使用的范围键的数组。我见过这么多例子,这是我第一次看到除了 new List() 之外对那行有任何关注。谢谢你。它仍然没有关闭锁定图标,但我返回的 id_token 实际上属于用户而不仅仅是客户端。
    猜你喜欢
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    • 2020-03-21
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 2021-07-08
    • 2019-12-11
    相关资源
    最近更新 更多