javascript - msal.js 包的 loginRedirect() 方法导致 'TypeError: Cannot read property ' then' of undefined'

标签 javascript microsoft-teams msal msal.js microsoft-graph-teams

我目前正在使用 msal.js 包,以便我可以为我自己的 Vue.js 应用程序使用 azure 授权。
到目前为止,我已经创建了一个 Teams 应用程序,可以在其中访问我的 Vue.js 网站,该网站使用 ngrok 进行隧道传输。

我在 Vue.js 中的代码如下所示(为了安全起见,我在这个 stackoverflow 帖子中用占位符替换了 clientId 和 authority):

import * as Msal from 'msal';

export default {
  signIn: async () => {

    const aDConfig = {
      auth: {
        clientId: 'AZUREAPPID',
        authority: 'https://login.microsoftonline.com/AZURETENANTID',
        redirectUri: 'https://login.microsoftonline.com/common/oauth2/nativeclient',
      },
      cache: {
        cacheLocation: 'localStorage',
        storeAuthStateInCookie: true,
      },
    };

    const aDAuth = new Msal.UserAgentApplication(aDConfig);

    const loginRequest = {
      scopes: ['user.read'],
    };

    await aDAuth.handleRedirectCallback((error, response) => {
      debugger;
      console.log(error);
      console.log(response);
    });

    await aDAuth.loginRedirect(loginRequest).then(async (loginResponse) => {
      console.log(loginResponse);

      debugger;
    }).catch((error) => {
      console.log(error);
    });
  },
};

基本上它所做的是设置要连接的天蓝色应用程序,然后尝试通过 loginRedirect() 方法静默登录。

但是当我尝试运行此代码时,在 loginRedirect() 方法处脚本停止并且我将收到错误消息:

enter image description here

由于 loginRequest 不为空,我不确定错误指的是什么。

这里可能是什么问题?

最佳答案

loginRedirect不返回 Promise(因为它使用户离开当前页面)。如果要处理重定向的结果,需要实现adAuth.handleRedirectCallback(callback) (这应该在页面加载时完成,在实例化 adAuth 之后立即执行),当 Msal 检测到从重定向流返回后正在加载页面时,将调用它。

见:https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-core/README.md#1-instantiate-the-useragentapplication

编辑:误读了您的代码,我看到您已经这样做了。所以只需删除 .thenloginRedirect你应该很好。

另外,handleRedirectCallback不返回 Promise,所以你不应该 await它。您需要实例化 Msal 并在 signIn 之外实现回调。功能(例如在页面加载时)。

import * as Msal from 'msal';

const aDConfig = {
    auth: {
      clientId: 'AZUREAPPID',
      authority: 'https://login.microsoftonline.com/AZURETENANTID',
      redirectUri: 'https://login.microsoftonline.com/common/oauth2/nativeclient',
    },
    cache: {
      cacheLocation: 'localStorage',
      storeAuthStateInCookie: true,
    }
};

const aDAuth = new Msal.UserAgentApplication(aDConfig);

const loginRequest = {
    scopes: ['user.read']
};

aDAuth.handleRedirectCallback((error, response) => {
    debugger;
    console.log(error);
    console.log(response);
});


export default {
  signIn: async () => {
    aDAuth.loginRedirect(loginRequest);
  }
};

关于javascript - msal.js 包的 loginRedirect() 方法导致 'TypeError: Cannot read property ' then' of undefined',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59299868/

相关文章:

sharepoint-online - MSAL AD token 对 SharePoint Online CSOM 无效

javascript - 悬停突出显示多个 div 标签

microsoft-teams - 团队机器人 : Updated message with a Card not properly displayed until chat is refreshed

angular - 错误的重定向到本地主机

typescript - b2c msal 用户没有现有的 session 和请求提示参数

javascript - authContext.login() 导致帧中出现 “Refused to display ‘login.microsoftonline.com…’,因为它将 ‘X-Frame-Options’ 设置为 ‘deny’ ”

javascript - 我怎样才能让这个日历正确关闭?

javascript - 如何使用 javascript 字典中的键查找值

javascript - 使用 jQuery 从 CSS 中删除 'disabled'

azure - MS Teams 通知机器人安装不会保留在 Azure blob 存储中