Angular with Azure AD B2C Audience Validation Failed

Deadly 提交于 2021-02-19 08:39:06

问题


I have an Anuglar5 spa frontend and ASP.NET Core API. Both secured by Azure AD B2C service. The angular application redirects correctly to the login page and signing in returns a token. When I try to call the API with the token I get;

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" is the Application ID of the frontend app. And "ee8b98a0-ae7a-38b2-9e73-d175df22ef4c" is the Application ID of the API.

My code;

`export class 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);
                });
        }
    );
}

}`

Using the token that is returned in Postman also returns the same error. My theory is that the URL I am using to call Azure AD B2C is the problem but looking through the docs I cannot find the problem.

Any help would be greatly appreciated.


回答1:


Kinda sounds like you are sending the Id token to the API (which is meant for your front-end) instead of an access token. You can debug the issue further by decoding the token you get at https://jwt.ms.

There the aud (audience) should match your API's id, and the scopes you asked should also be there.



来源:https://stackoverflow.com/questions/50104627/angular-with-azure-ad-b2c-audience-validation-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!