简体   繁体   English

登录到应用程序后,带有MSAL和Angular的Azure AD B2C会立即重定向到登录页面

[英]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. 我有一个使用ASP.NET核心Web API的Angular应用程序。 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. 我使用ADAL进行身份验证,但现在应用程序也应该可供客户访问,因此我删除了ADAL代码并添加了与MSAL相关的代码。 I created an Azure AD B2C tenant and used those credentials in the app. 我创建了一个Azure AD B2C租户,并在应用程序中使用了这些凭据。 When I run the app now, I am redirected to Microsoft login page which is perfect. 当我现在运行应用程序时,我被重定向到Microsoft登录页面,这是完美的。 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. 我没有时间在Dev工具中调试问题。 The code in msal.service.ts file is below msal.service.ts文件中的代码如下

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 auth.service.ts中的代码如下所示

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 这个你能帮我吗

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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