首页 > 解决方案 > SwaggerUI 中是否支持多个 oauth2 身份验证登录提供程序?

问题描述

我可以让多个登录选项出现在 SwaggerUI 中,代码类似于

    services.AddSwaggerGen(c =>
    {
        c.AddSecurityDefinition("Google", new OAuth2Scheme
        {
            Description = "Google",
            Type = "oauth2",
            Flow = "implicit",
            TokenUrl = "https://www.googleapis.com/oauth2/v3/token",
            AuthorizationUrl = "https://accounts.google.com/o/oauth2/auth"
        });

        c.AddSecurityDefinition("Microsoft", new OAuth2Scheme
        {
            Description = "Microsoft",
            Type = "oauth2",
            Flow = "implicit",
            TokenUrl = "blah",
            AuthorizationUrl = "blah"
        });

        c.AddSecurityDefinition("Bearer", new ApiKeyScheme
        {
            Description = "Standard Authorization header using the Bearer scheme. Example: \"bearer {token}\"",
            In = "header",
            Name = "Authorization",
            Type = "apiKey"
        });

        c.OperationFilter<SecurityRequirementsOperationFilter>();

和类似的东西

    app.UseSwaggerUI(c =>
    {
        c.OAuthClientId(this.Configuration["Authentication:Google:ClientId"]);
        c.OAuthClientSecret(this.Configuration["Authentication:Google:ClientSecret"]);
        c.OAuthAppName("blah");
        c.OAuthScopeSeparator(string.Empty);
        c.OAuthAdditionalQueryStringParams(new { audience = this.Configuration["Authentication:Google:ClientId"], scope = "openid profile email" });
        c.OAuthUseBasicAuthenticationWithAccessCodeGrant();
    });

如果我只配置了一个 oauth 提供程序,它可以正常工作。问题是 Google 和 Microsoft 登录选项都将使用 UseSwaggerUI 方法中声明的默认 ClientId 和 ClientSecret 等。

有没有办法在 Swagger UI 中支持多个登录提供程序及其相关配置?

标签: asp.net-coreoauth-2.0swaggerswagger-ui

解决方案


推荐阅读