【发布时间】:2019-03-21 16:17:50
【问题描述】:
我们正在为 MS Graph API 构建一个 Web API 包装器。
我想使用 Swagger 来测试我的 API。但我无法正确配置。我不断收到错误的请求,没有其他线索。我无法在这台公司笔记本电脑上安装 Fiddler 或其他工具来帮助我进行调查。
这是配置 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
到
Type = SwaggerSecuritySchemeType.OAuth2
我还删除了一堆东西,比如 ff 行
settings.OAuth2Client.AdditionalQueryStringParameters.Add("response_type", "code id_token");
settings.OAuth2Client.AdditionalQueryStringParameters.Add("nonce", "AnyValueShouldBeRandom");
它现在正在工作。
至少在外面。
Swagger 告诉我我已经通过身份验证:
但是当我运行应用程序时,HttpContext.User.Identity.IsAuthenticated 告诉我我不是。
同样的问题:我做错了什么?
【问题讨论】:
-
如果使用 v2 端点,请务必在正确的应用注册门户中注册您的应用。
-
我很确定我做到了,事实上,我错过了在问题中提到,我已成功重定向到 MS 登录屏幕。
标签: c# asp.net-core swagger-ui azure-ad-graph-api nswag