MSAL Angular 7:密码重置实现

时间:2019-01-21 08:13:31

标签: javascript angular authentication msal msal.js

我正在使用此MSAL库(https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-angular)进行身份验证。

要重置密码,我的代码如下:

// app.component.ts

constructor(private authService: MsalService)
{
 if (this.forgotPassword()) {
            this.navigateToForgotPassword();
        } else {
        this.authService
            .acquireTokenSilent(MsalHelperService.getMsalConfigurationSettingValue('consentScopes'))
            .then(token => {
                if (this.authService.getUser()) {
                    this.userService.setLoggedIn(true);
                    this.navigateToLandingPage();
                } else {
                    this.userService.setLoggedIn(false);
                    this.login();
                }
            })
            .catch(error => {
                this.userService.setLoggedIn(false);
                this.login();
            });
    }
...
}

// Determine if user clicked "Forgot Password"
forgotPassword() {
        const storage = this.authService.getCacheStorage();
        const authError: string = storage.getItem('msal.login.error') ? storage.getItem('msal.login.error') : null;
        if (authError && authError.indexOf('AADB2C90118') > -1) {
            return true;
        }

        return false;
    }

navigateToForgotPassword() {
        this.authService.authority = this.authService.authority.replace('b2c_1a_signupsignin', 'b2c_1a_passwordreset');
        this.authService.loginRedirect(MsalHelperService.getMsalConfigurationSettingValue('consentScopes'));
    }

到目前为止,一切正常。

重置密码后,用户将被定向到app.component,然后调用loginRedirect()来显示登录表单。

返回到app.component时,记录以下错误:

“拒绝在一个框架中显示'https://...signupsignin/',因为它将'X-Frame-Options'设置为'deny'”。

理想情况下,我想在密码重置后自动登录用户。

请告知这是否完全有可能,或者至少我该如何摆脱上面的错误而不必修改MSAL库。

2 个答案:

答案 0 :(得分:2)

自动登录仍然是一个问题,但通过注销loginSyccess解决了该错误。

this.loginSuccessSubscription = this.broadcastService.subscribe('msal:loginSuccess', payload => {
                // Temporary solution to avoid 'X-Frame-Options' error on password reset. MSAL not yet supporting auto login after password reset.
                if (this.resetPassword()) {
                    this.logout();
                }

               ...
            });

    // Check claim
    resetPassword() {
            return document.referrer.toLowerCase().indexOf('b2c_1a_passwordreset') > -1;
        }

答案 1 :(得分:1)

根据您的回答,我做了类似的事情:

if (payload._errorDesc && payload._errorDesc.indexOf('AADB2C90118') !== -1) {
    console.log('Set recovery flow to true');
    console.log('Redirecting to password recovery page');
    localStorage.setItem('custom.recovery.password.flow', 'true');
    msalService.authority = `https://login.microsoftonline.com/tfp/${environment.tenant}/b2c_1_reset_password/v2.0/`;
    msalService.loginRedirect();
  }
});
this.broadcastService.subscribe('msal:loginSuccess', payload => {
  if(localStorage.getItem('custom.recovery.password.flow') === 'true'){
    console.log('Set recovery to false');
    console.log('Redirecting to login page');
    localStorage.setItem('custom.recovery.password.flow', 'false');
    msalService.logout();
  }
});