【问题标题】:.NET Core Web Api Azure AD and Swagger not authenticating.NET Core Web Api Azure AD 和 Swagger 未进行身份验证
【发布时间】:2019-10-28 20:38:19
【问题描述】:

我正在尝试使用 Azure AD 身份验证和 Swagger 设置 .NET Core Web API,但在尝试进行身份验证时会出错。

我正在关注本指南:http://www.sharepointconfig.com/2018/08/configure-swagger-to-authenticate-against-azure-ad/

ConfigureServices 包含以下代码:

        services.AddSwaggerGen(c =>
        {
            c.AddSecurityDefinition("oauth2", //Name the security scheme
                   new OpenApiSecurityScheme
                   {
                       Type = SecuritySchemeType.OAuth2,
                       Flows = new OpenApiOAuthFlows() { Implicit = new OpenApiOAuthFlow() },
                       Scheme = "oauth2",
                       OpenIdConnectUrl = new Uri($"https://login.microsoftonline.com/" + _configuration["AzureAD:TenantId"] + "/oauth2/authorize"),
                   });
            c.AddSecurityRequirement(new OpenApiSecurityRequirement{
            {
                new OpenApiSecurityScheme{
                    Reference = new OpenApiReference{
                        Id = "oauth2",
                        Type = ReferenceType.SecurityScheme
                    }
                },new List<string>()
                }
            });
        });

配置包含此代码:

                app.UseSwaggerUI(c =>
            {
                c.OAuthClientId(_configuration["Swagger:ClientId"]);
                c.OAuthClientSecret(_configuration["Swagger:ClientSecret"]);
                c.OAuthRealm(_configuration["AzureAD:ClientId"]);
                c.OAuthAppName("WerfRegistratie V1");
                c.OAuthScopeSeparator(" ");
                c.OAuthAdditionalQueryStringParams(new Dictionary<string, string> { { "resource", _configuration["AzureAD:ClientId"] } });
            });

我一直遇到的问题是: Swagger authentication error

看起来当我尝试单击授权按钮时,Swagger 中的变量未填写,我一直在 SwaggerUI 中尝试不同的设置,但这种情况一直在发生。

【问题讨论】:

    标签: c# azure asp.net-core swagger swashbuckle


    【解决方案1】:

    我们是这样配置的:

    var authority = "https://login.microsoftonline.com/your-aad-tenant-id";
    o.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
    {
        Type = SecuritySchemeType.OAuth2,
        Flows = new OpenApiOAuthFlows
        {
            Implicit = new OpenApiOAuthFlow
            {
                Scopes = new Dictionary<string, string>
                {
                    ["Stuff.Read"] = "Read stuff" // TODO: Replace with your scopes
                },
                AuthorizationUrl = new Uri(authority + "/oauth2/authorize")
            }
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-27
      • 2020-03-09
      • 2020-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-15
      • 2021-10-05
      相关资源
      最近更新 更多