【问题标题】:How to get the current user in .net core web api post request?如何在.net core web api post请求中获取当前用户?
【发布时间】:2021-03-26 07:05:43
【问题描述】:

我正在开发一个 Angular 项目,该项目使用 http 和 web api(.net core 3.1) 连接在 Azure 中注册和部署的数据库,并且我只允许访问当前在 Azure 活动目录中的一个用户,并且在发布请求中,我想获取正在保存/编辑记录的当前用户。我曾尝试使用 MSAL,但 sessonstorage.getitem() 为空。我是 MSAL 的新手,所以请告诉我它是如何工作的,如果有人逐步解释使用 MSAL 和获取当前用户,我将不胜感激。你能帮我解决这个问题吗?提前致谢!!

注意如果有其他方法可以获取在 .net core 3.1 中的 Azure 活动目录中授予访问权限的当前用户(不是通过 Windows 身份验证),请告诉我..

【问题讨论】:

    标签: angular azure-active-directory msal webapi .net-core-3.1


    【解决方案1】:

    您可以使用 MSAL 试试这个tutorial。登录后显示带有myMSALObj.getAccount()的欢迎消息。

    const myMSALObj = new Msal.UserAgentApplication(msalConfig);
    
    function signIn() {
      myMSALObj.loginPopup(loginRequest)
        .then(loginResponse => {
          console.log('id_token acquired at: ' + new Date().toString());
          console.log(loginResponse);
    
          if (myMSALObj.getAccount()) {
            showWelcomeMessage(myMSALObj.getAccount());
          }
        }).catch(error => {
          console.log(error);
        });
    }
    
    function showWelcomeMessage(account) {
    
        // Reconfiguring DOM elements
        cardDiv.classList.remove('d-none');
        welcomeDiv.innerHTML = `Welcome ${account.name}`;
        signInButton.classList.add('d-none');
        signOutButton.classList.remove('d-none');
    }
    

    【讨论】:

      猜你喜欢
      • 2018-02-17
      • 2017-06-11
      • 2020-07-19
      • 2020-01-04
      • 1970-01-01
      • 2017-07-10
      • 2021-11-20
      • 1970-01-01
      • 2017-01-21
      相关资源
      最近更新 更多