繁体   English   中英

登录后如何获取用户名?我正在尝试使用 MSAL (@azure/msal-angular) 进行 Azure 登录

[英]How to get username after logged in?.I am trying with MSAL (@azure/msal-angular) for Azure Signin

我正在尝试使用 MSAL (@azure/msal-angular) 进行 Azure 登录和 angular 的新登录。我需要在登录后获取用户名并将其显示在 UI 中。提前致谢

您可以使用myMSALObj.getAccount().userName来获取用户名。

function showWelcomeMessage() {
    var divWelcome = document.getElementById('WelcomeMessage');
    divWelcome.innerHTML = 'Welcome ' + myMSALObj.getAccount().userName + " to Microsoft Graph API";
    var loginbutton = document.getElementById('SignIn');
    loginbutton.innerHTML = 'Sign Out';
    loginbutton.setAttribute('onclick', 'signOut();');
}

有关更多详细信息,请参见此处

您可以从MsalService检索用户,例如:

const accountInfo: AccountInfo | null = this.msalService.instance.getActiveAccount();

建议等到身份验证过程完成后再调用它。 这可以通过检查MsalBroadcastService来完成:

const account: Observable<AccountInfo | null> = this.msalBroadcastService
    .inProgress$
    .pipe(
        filter(status => status == InteractionStatus.None),
        map(() => {
            const instance: IPublicClientApplication = this.msalService.instance;
            const activeAccount: AccountInfo | null = instance.getActiveAccount();
            const accounts: AccountInfo[] = instance.getAllAccounts();
            if (activeAccount != null) return activeAccount;
            if (accounts.length > 0) {
                const [firstAccount] = accounts;
                instance.setActiveAccount(firstAccount);
                return firstAccount;
            }
            return null;
        })
    );
  
    });

暂无
暂无

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

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