【问题标题】:Angular with Azure AD B2C Audience Validation Failed带有 Azure AD B2C 受众验证的 Angular 失败
【发布时间】:2018-10-10 19:00:06
【问题描述】:

我有一个 Anuglar5 spa 前端和 ASP.NET Core API。两者均由 Azure AD B2C 服务保护。角度应用程序正确重定向到登录页面并登录返回一个令牌。当我尝试使用获得的令牌调用 API 时;

AuthenticationFailed: IDX10214: Audience validation failed. Audiences: '627684f5-5011-475a-9cbd-55fcdcdf369e'. Did not match: validationParameters.ValidAudience: 'ee8b98a0-ae7a-38b2-9e73-d175df22ef4c' or validationParameters.ValidAudiences: 'null'.

“627684f5-5011-475a-9cbd-55fcdcdf369e”是前端应用程序的应用程序 ID。 “ee8b98a0-ae7a-38b2-9e73-d175df22ef4c”是API的Application ID。

我的代码;

`导出类 MSALService {

private applicationConfig: any = {
    clientID: '627684f5-5011-475a-9cbd-55fcdcdf369e',
    authority: 'https://login.microsoftonline.com/tfp/mytenant.onmicrosoft.com/B2C_1_my_signin_signup',
    b2cScopes: ['https://meeblitenant.onmicrosoft.com/api/myapp_read', 'https://meeblitenant.onmicrosoft.com/api/myapp_write'],
    redirectUrl: 'http://localhost:4200/'
};

private app: any;
public user: any;

constructor() {

    this.app = new UserAgentApplication(this.applicationConfig.clientID, this.applicationConfig.authority,
        (errorDesc, token, error, tokenType) => {
           console.log(token);
        },
        { redirectUri: this.applicationConfig.redirectUrl }
    );
}

public login() {

    let tokenData = '';
    this.app.loginRedirect(this.applicationConfig.b2cScopes).then(data => { tokenData = data; });
}

public getUser() {

    const user = this.app.getUser();

    if (user) {

        return user;
    } else {

        return null;
    }
}

public logout() {

    this.app.logout();
}

public getToken() {

    return this.app.acquireTokenSilent(this.applicationConfig.b2cScopes)
        .then(accessToken => {
            console.log(accessToken);
            return accessToken;
        }, error => {
            return this.app.acquireTokenPopup(this.applicationConfig.b2cScopes)
                .then(accessToken => {
                    return accessToken;
                }, err => {
                    console.error(err);
                });
        }
    );
}

}`

使用 Postman 中返回的令牌也会返回相同的错误。我的理论是我用来调用 Azure AD B2C 的 URL 是问题,但通过查看文档我找不到问题。

任何帮助将不胜感激。

【问题讨论】:

  • 有点像您将 Id 令牌发送到 API(这意味着您的前端)而不是访问令牌。您可以通过解码在jwt.ms 获得的令牌来进一步调试问题。 aud(观众)应该与您的 API 的 id 匹配,并且您询问的范围也应该在那里。
  • @juunas Damn .. 你的评论应该是一个答案并且被接受。我浪费了一整天,直到我找到你的答案。谢谢!
  • 好吧,我做到了:)

标签: asp.net-core angular5 azure-ad-b2c


【解决方案1】:

有点像您将 Id 令牌发送到 API(用于您的前端)而不是访问令牌。您可以通过解码您在https://jwt.ms 获得的令牌来进一步调试问题。

aud(观众)应该与您的 API 的 id 匹配,并且您询问的范围也应该在那里。

【讨论】:

  • 我在 HttpContext - Request - Headers.- Values 中找到了我的令牌
猜你喜欢
  • 1970-01-01
  • 2021-10-16
  • 2018-12-10
  • 2021-11-28
  • 2017-09-02
  • 2020-07-24
  • 2022-08-16
  • 1970-01-01
  • 2019-06-25
相关资源
最近更新 更多