简体   繁体   English

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

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

I am trying with MSAL (@azure/msal-angular) for Azure Signin and new to angular.I need to get username after logged in and display it in UI.Thanks in advance我正在尝试使用 MSAL (@azure/msal-angular) 进行 Azure 登录和 angular 的新登录。我需要在登录后获取用户名并将其显示在 UI 中。提前致谢

You could use myMSALObj.getAccount().userName to get userName.您可以使用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();');
}

For more details, see here .有关更多详细信息,请参见此处

You can retrieve the user from MsalService , for example with:您可以从MsalService检索用户,例如:

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

It's recommended to wait until the authentication process is complete before you invoke this.建议等到身份验证过程完成后再调用它。 This can be done by checking MsalBroadcastService :这可以通过检查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.

相关问题 如何使用@azure/msal-angular 获取用户的 AAD 会员资格 - How to get user's AAD memberships with @azure/msal-angular 有什么方法可以使用@azure/msal-angular 登录用户 azure 订阅 ID? - Is there any way to get logged in user azure subscription Id's using @azure/msal-angular? @ azure / msal-angular的元数据版本不匹配 - MetaData version mismatch for @azure/msal-angular 通过 angular 8 中的@azure/msal-angular 库获取 azure 广告用户组 - Get azure ad user group by @azure/msal-angular library in angular 8 当我使用 Azure static web 应用程序时,为什么 msal-angular `loginPopup` 弹出它自己的页面 - Why does msal-angular `loginPopup` pop ups it's own page when I am using Azure static web app @azure/msal-angular package 会自动刷新令牌吗? - Does the @azure/msal-angular package automatically refresh token? 如何侦听 MSAL-Angular 令牌重新身份验证请求? - How do I listen for MSAL-Angular token reauthentiation requests? Azure Ad B2C:如何使用 msal-angular 传递在重定向中仍然可用的参数 url - Azure Ad B2C: How to use use msal-angular to pass parameter which still available in redirect url @azure/msal-angular 1.1.1 有 TypeError: glob pattern string required - @azure/msal-angular 1.1.1 has TypeError: glob pattern string required 注销时绕过帐户选择屏幕(注销)@azure/msal-angular V2 - Bypass the account selection screen while sign out(log out) @azure/msal-angular V2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM