如何授权Swagger使用MS Graph API

5
我们正在构建用于 MS Graph API 的 Web API 封装器。
我想使用 Swagger 来测试我的 API。但是我无法正确配置它。我一直得到“Bad Request”错误,却没有其他线索。我不能在这台公司笔记本电脑上安装 Fiddler 或其他工具来帮助我进行调查。
这是错误: enter image description here 以下是配置 Swagger 的代码:
app.UseSwaggerUi3WithApiExplorer(settings =>
{
    settings.GeneratorSettings.DefaultPropertyNameHandling = PropertyNameHandling.CamelCase;
    settings.PostProcess = document =>
    {
        document.Info.Title = "App title";
        document.Info.Description = "App description";
    };

    settings.OAuth2Client = new OAuth2ClientSettings
    {
        ClientId = [clientid]
        ClientSecret = [clientsecret]
        AppName = "app_name",
    };
    settings.OAuth2Client.AdditionalQueryStringParameters.Add("response_type", "code id_token");
    settings.OAuth2Client.AdditionalQueryStringParameters.Add("nonce", "AnyValueShouldBeRandom");

    settings.GeneratorSettings.DocumentProcessors.Add(new SecurityDefinitionAppender("Auth Token", new SwaggerSecurityScheme
    {
        Type = SwaggerSecuritySchemeType.OpenIdConnect,
        Description = "Swagger OAuth2",
        OpenIdConnectUrl = "https://login.microsoftonline.com/[tenantid]/v2.0/.well-known/openid-configuration",
        Flow = SwaggerOAuth2Flow.Implicit,
        AuthorizationUrl = "https://login.microsoftonline.com/[tenantid]/oauth2/v2.0/authorize",
        TokenUrl = "https://login.microsoftonline.com/[tenantid]/oauth2/v2.0/token",
        In = SwaggerSecurityApiKeyLocation.Header,

        Scopes = new Dictionary<string, string>
        {
            { "api://[api]/user_impersonation", "" },
            { "user.read", "" },
            { "openid", "" },
            { "email", "" },
            { "profile", "" },
            { "roles", "" }
        }
    }));

    settings.GeneratorSettings.OperationProcessors.Add(new OperationSecurityScopeProcessor("oauth2"));

});

我的问题是我做错了什么?

我从今早开始就一直在努力解决这个问题。非常感谢任何帮助。

谢谢!

更新于2019年3月21日

我弄清楚了。

将此代码更改为

Type = SwaggerSecuritySchemeType.OpenIdConnect

to

Type = SwaggerSecuritySchemeType.OAuth2

我还删除了很多类似以下行的内容

settings.OAuth2Client.AdditionalQueryStringParameters.Add("response_type", "code id_token");
settings.OAuth2Client.AdditionalQueryStringParameters.Add("nonce", "AnyValueShouldBeRandom");

它现在可以工作了。

至少从外部来看是这样的。

Swagger 告诉我已经通过身份验证:

enter image description here

但是当我运行应用程序时,HttpContext.User.Identity.IsAuthenticated 告诉我没有通过身份验证。

同一个问题:我做错了什么?


如果使用v2端点,请确保在正确的应用程序注册门户中注册您的应用程序。 - phatoni
我相当确定我确实这样做了,而且我在问题中忘记提到的是,我已经成功地重定向到了 MS 登录界面。 - jokab
1个回答

1

最终我可以回答自己的问题了。

这一次我不会对自己太苛刻,因为解决方法并不是很明显,至少对我来说是这样。

显然,

settings.GeneratorSettings.OperationProcessors

应该有一个匹配的


settings.GeneratorSettings.DocumentProcessors

如果我没有足够努力地搜索或文档确实不够易于访问,那么这部分责任应该由我承担。

但是这一行

settings.GeneratorSettings.OperationProcessors.Add(new OperationSecurityScopeProcessor("oauth2"));

需要匹配,因此请替换以下内容。
settings.GeneratorSettings.DocumentProcessors.Add(new SecurityDefinitionAppender("Auth Token", new SwaggerSecurityScheme

使用

settings.GeneratorSettings.DocumentProcessors.Add(new SecurityDefinitionAppender("oauth2", new SwaggerSecurityScheme

我希望这能帮助其他人。

如果NSwag的wiki不够清晰,并且对其他用户有价值,请更新它。 - Rico Suter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接