[英]How to get user's AAD memberships with @azure/msal-angular
[英]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.