Microsoft Teams 身份验证:无法在团队机器人 (V4 SDK) 中检索令牌

Microsoft Teams Authentication : Unable to retrieve token in teams bot ( V4 SDK)

我有一个 Microsoft Teams bot,我正在尝试使用 AAD(v2 端点) 对用户进行身份验证。该机器人使用 V4 SDK 和 OauthPrompt。我可以登录但无法获取令牌。

我正在使用 OAuth 卡片。卡片正在 Teams 中呈现。单击登录按钮后,它将转到 Microsoft 登录页面。用户登录后,机器人停止响应,因此我无法获取令牌。

我正在试用的样本是: https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/csharp_dotnetcore/18.bot-authenticationhttps://github.com/microsoft/BotBuilder-Samples/tree/master/samples/csharp_dotnetcore/24.bot-authentication-msgraph.

两个示例都在模拟器中运行。

这是代码片段:

               AddDialog(new OAuthPrompt(
            nameof(OAuthPrompt),
            new OAuthPromptSettings
            {
                ConnectionName = ConnectionName,
                Text = "Please Sign In",
                Title = "Sign In",
                Timeout = 300000, // User has 5 minutes to login (1000 * 60 * 5)
            }));


         AddDialog(new ConfirmPrompt(nameof(ConfirmPrompt)));

        AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
        {
            PromptStepAsync,
            LoginStepAsync,
            DisplayTokenPhase1Async,
            DisplayTokenPhase2Async,
        }));

 private async Task<DialogTurnResult> PromptStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
           return await stepContext.BeginDialogAsync(nameof(OAuthPrompt), null, cancellationToken);
        }


它永远不会达到代码:

        private async Task<DialogTurnResult> LoginStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {
        // Get the token from the previous step. Note that we could also have gotten the
        // token directly from the prompt itself. There is an example of this in the next method.
        var tokenResponse = (TokenResponse)stepContext.Result;
        if (tokenResponse != null)
        {
            await stepContext.Context.SendActivityAsync(MessageFactory.Text("You are now logged in."), cancellationToken);
            return await stepContext.PromptAsync(nameof(ConfirmPrompt), new PromptOptions { Prompt = MessageFactory.Text("Would you like to view your token?") }, cancellationToken);
        }

        await stepContext.Context.SendActivityAsync(MessageFactory.Text("Login was not successful please try again."), cancellationToken);
        return await stepContext.EndDialogAsync(cancellationToken: cancellationToken);
    }

我预计用户登录后代码中会收到令牌,但机器人只是停止响应。

这似乎是 Bot 框架中的一个错误,但已经有一个 pull request 来解决这个问题。您可以找到拉取请求 here.

编辑:如您所见here

,他们现在编辑了两个身份验证示例以解决此问题

这就是我让它工作的方式。

public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
    {

        await base.OnTurnAsync(turnContext, cancellationToken);

        // Save any state changes that might have occured during the turn.
        await ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);
        await UserState.SaveChangesAsync(turnContext, false, cancellationToken);

           if (turnContext.Activity.Name == "signin/verifyState")
            {

                await Dialog.Run(turnContext, ConversationState.CreateProperty<DialogState>(nameof(DialogState)), cancellationToken);

            }

      }

然后进入Waterfall的LoginStepAsync步骤。