【发布时间】:2019-09-14 02:27:54
【问题描述】:
我正在尝试使用 Azure Active Directory V2 在我的 AspNetCore 2.1 应用程序中设置 Swagger,但我似乎无法正确设置。我能够配置设置,以便 swagger 提示、重定向并成功验证我的客户端/用户,但是当将承载令牌传递给服务器时会导致错误 Bearer error="invalid_token", error_description="The signature is invalid"。我已经使用我正在尝试使用其所有配置的项目创建了一个 GitHub 存储库 (https://github.com/alucard112/auth-problem)
通过将资源设置为 AAD 应用程序的客户端 ID,我已设法使 V1 端点正常工作,这导致 JWT 令牌将“aud”设置为应用程序客户端 ID。在 V2 端点中,“aud”被设置为我认为的 Graph API 资源“00000003-0000-0000-c000-000000000000”。我相信这是我目前的问题,尽管我不是 100% 确定。 V2 端点似乎没有像 V1 那样定义受众的方法,除非当然有我这边的一些疏忽。
我的启动文件结构如下:
身份验证设置如下:
services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme)
.AddAzureADBearer(options => Configuration.Bind("AzureAd", options));
services.Configure<JwtBearerOptions>(AzureADDefaults.JwtBearerAuthenticationScheme, options =>
{
options.Authority = $"https://login.microsoftonline.com/{tenantId}";
options.TokenValidationParameters = new TokenValidationParameters
{
// In multi-tenant apps you should disable issuer validation:
ValidateIssuer = false,
// In case you want to allow only specific tenants,
// you can set the ValidIssuers property to a list of valid issuer ids
// or specify a delegate for the IssuerValidator property, e.g.
// IssuerValidator = (issuer, token, parameters) => {}
// the validator should return the issuer string
// if it is valid and throw an exception if not
};
});
并且大摇大摆的设置如下:
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info
{
Title = "Protected Api",
});
c.OperationFilter<SecurityRequirementsOperationFilter>();
//IMATE - StevensW
// Define the OAuth2.0 scheme that's in use (i.e. Implicit Flow)
c.AddSecurityDefinition("oauth2", new OAuth2Scheme
{
Type = "oauth2",
Flow = "implicit",
AuthorizationUrl = $"https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/authorize",
TokenUrl = $"https://login.microsoftonline.com/common/{tenantId}/v2.0/token",
Scopes = new Dictionary<string, string>
{
{ "openid", "Unsure" },
{ "profile", "Also Unsure" }
}
});
});
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.OAuthClientId(Configuration.GetValue<string>("AzureAd:ClientId"));
c.OAuthAppName("Protected API");
//c.OAuthUseBasicAuthenticationWithAccessCodeGrant();
//c.OAuthClientSecret(Configuration.GetValue<string>("AzureAd:ClientId"));
});
我希望将 swagger UI 配置为使用 AAD 的 V2 端点并允许多租户登录,从而允许执行成功验证的 API 调用。任何帮助或指导将不胜感激。
【问题讨论】:
-
感谢 Jokab,我现在正在查看您的帖子,看看是否有任何帮助。我一直在阅读 AAD 文档以尝试更好地理解该过程,现在可以确认“aud”确实需要与我的客户 ID 匹配。所以这将是第一个要解决的问题 (docs.microsoft.com/en-us/azure/active-directory/develop/…) - 只是为了兴趣添加这个
标签: c# asp.net-core azure-active-directory swagger swashbuckle