首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MSAL Angular 7:密码重置实现

MSAL Angular 7:密码重置实现
EN

Stack Overflow用户
提问于 2019-01-21 16:13:31
回答 2查看 3.9K关注 0票数 3

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

对于密码重置,我的代码如下:

代码语言:javascript
复制
// 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库的情况下摆脱上面的错误。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-07 13:34:05

自动登录仍然是一个问题,但我通过在loginSuccess上注销解决了这个错误。

代码语言:javascript
复制
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;
}
票数 3
EN

Stack Overflow用户

发布于 2019-02-07 22:00:11

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

代码语言:javascript
复制
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();
  }
});
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54285749

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档