首页 > 解决方案 > 微软团队身份验证

问题描述

我开发了一个自定义团队应用程序,它有一个基本选项卡,用户必须在其中登录才能查看某些内容。身份验证通过 Oauth2 代码流,使用外部 npm 包 (@openid/appauth)。当用户单击登录按钮时,我创建了此代码以打开弹出窗口:

     login = () => {
            const successCallback = (result) => {
                this.props.setLogged(result);
            }
            const failureCallback = (error) => {
                logger.warn(JSON.stringify(error));
                this.props.setError(error);
            }
            authClient.teamsAuthRequest(this.props.user.PartitionKey, this.props.user.RowKey, successCallback, failureCallback);
        } 

此功能:teamAuthRequest 只是用于调用授权端点的配置:

     teamsAuthRequest: async (tid, aaId, successCallback, failureCallback) => {
                const config = await createConfiguration();
                const request = createAuthRequest(tid, aaId);
                teamsHandler.performAuthorizationRequest(config, request, successCallback, failureCallback);
            }

它通过以下代码调用一个打开弹出窗口的函数:

            microsoftTeams.authentication.authenticate({
                url: url,
                width: 600,
                height: 550,
                successCallback: function (result) {
                    successCallback(result);
                },
                failureCallback: function (reason) {
                    failureCallback(reason)
                }
            });

如果我使用团队网络,它会正确打开弹出窗口并在登录后关闭它。

如果我在团队桌面中使用相同的应用程序,它会正确打开弹出窗口,但它永远不会调用 notifySuccess 函数,也永远不会关闭弹出窗口。

当我收到来自外部提供商的回调以接收授权代码时,我运行此代码:

      if (response) {
        authClient.getToken(request && request.internal && request.internal.code_verifier, response.code)
          .then((oResponse) => {
            user = {
              PartitionKey: request.extras.tid,
              RowKey: request.extras.aaId,
              AccessToken: oResponse.accessToken,
              RefreshToken: oResponse.refreshToken
            }
              microsoftTeams.authentication.notifySuccess(user)
          })
          .catch(oError => {
            microsoftTeams.authentication.notifyFailure(oError)
          });
      }

我该如何解决?

标签: authenticationoauthcallbacktabsmicrosoft-teams

解决方案


Teams 基本上会使用microsoftTeams.authentication.authenticate. 结果,即使您看到弹出窗口,您实际上也想使用重定向流。请在此处查看说明此问题的示例应用程序

https://github.com/pnp/teams-dev-samples/tree/master/samples/tab-sso


推荐阅读