简体   繁体   中英

Azure AD B2C with MSAL and Angular redirects to login page immediately after logging into the app

I have an Angular app that uses ASP.NET core Web API. I used ADAL for authentication but now the app should also be accessible to customers and hence I removed the ADAL code and added MSAL related code. I created an Azure AD B2C tenant and used those credentials in the app. When I run the app now, I am redirected to Microsoft login page which is perfect. But when I login after entering email and password, it logs into the app and immediately redirects me to the login page again. I don't get time to even debug the issue in Dev tools. The code in msal.service.ts file is below

private applicationConfig: any = {
    clientID: 'XXXXXXXXXXXXXXXXXXXXXXXXXXX',
    authority: 'XXXXXXXXXXXXXXXXXXXXXXXXXX',
    b2cScopes: ['XXXXXXXXXXXXXXXXXXXXXXXXX'],
    redirectUrl: 'XXXXXXXXXXXXXXXXXXXXXXXX'
};

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 });
    // this.app.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);
                });
        });
}

The code in auth.service.ts is shown below

constructor(private msalService: MSALService, public jwtHelper: JwtHelper) { }

public getToken(): string {
    const token: string = window.sessionStorage.getItem('msal.idtoken');
    return token;
}

public login() {
    const token = this.getToken();
    if (token == null || token === undefined || token === 'null') {
        this.msalService.login();
    }
}

public getTokenDecoded(): any
{
  return this.jwtHelper.decodeToken(this.getToken());
}

I spent a lot of time on this but unable to identify the exact cause of the issue. Please help me out with this

To debug these problems turn on preserve log in the console and network tabs:

控制台窗口截图

网络标签截图


There is probably an error returned in the URL which should be visible in the network tab.

ex:

https://jwt.ms/#error=server_error&error_description=AADB2C90161%3a+A+self-asserted+send+response+has+failed+with+reason+%27Internal+Server+Error%27.%0d%0aCorrelation+ID%3a+bc145550-912a-4f35-b833-334cf1ace96d%0d%0aTimestamp%3a+2018-01-08+01%3a48%3a43Z%0d%0a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM