MS Bot框架不记得身份验证

时间:2019-05-03 13:18:45

标签: c# .net azure-active-directory botframework

我正在使用v4框架开发MS Bot,并且正在尝试设置身份验证。问题是我可以进行身份​​验证,但是如果我想通过重新提示对话框(找到here)来重新捕获用户身份验证令牌,那么我确实会得到一个新的登录提示符,而不仅仅是获取令牌。

这是尝试的方法:

  • 使用Azure AD v1和v2
  • 搜索文档(但始终不推荐使用,或者与链接中的内容相同)

我的代码(如果您有其他需要的话):

// Register the Promt
AddDialog(Prompt(ConnectionName));

// Prompts the user to log in using the OAuth provider specified by the connection name.
// Prompt definition
public static OAuthPrompt Prompt(string connectionName) => new OAuthPrompt(
       LoginPromptName,
       new OAuthPromptSettings
       {
           ConnectionName = connectionName,
           Text = "Please login",
           Title = "Login",
           Timeout = 300000, // User has 5 minutes to login
       });

private async Task<DialogTurnResult> PromptStepAsync(WaterfallStepContext context, CancellationToken cancellationToken)
{
    var state = await UserProfileAccessor.GetAsync(context.Context);
    return await context.BeginDialogAsync(LoginPromptName, cancellationToken: cancellationToken);
}

private async Task<DialogTurnResult> LoginStepAsync(WaterfallStepContext context, CancellationToken cancellationToken)
{
    var loginState = await UserProfileAccessor.GetAsync(context.Context);
    // 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)context.Result;
    if (tokenResponse != null)
    {
       * DOES SOMETHING WHEN LOGGED IN*
    }
    else
    {
       * DOES SOMETHING WHEN LOGIN FAILED *
    }


    /* !! 
       HERE IS THE PROBLEM IN STEAD OF CHECKING IF THE USER 
       IS ALREADY LOGGED IN AND JUST RETRIEVING THE TOKEN I
       GET A NEW LOGIN SCREEN.
       !!
    */
    var token2Response = await context.BeginDialogAsync(LoginPromptName, null, cancellationToken)


}

因此,基本上,我想检索用户令牌,以便在用户登录时可以检查不同的方法和对话框。

1 个答案:

答案 0 :(得分:1)

这是由于存在以下错误:OAuthPrompt requires user credentials when message contains guid or 6-digit number,该错误当前有一个Pull Request要修复。

目前的解决方法是在结束对话框之前确保漫游器获得任何其他类型的用户响应。带有以下内容的示例does it here

return await stepContext.PromptAsync(nameof(ConfirmPrompt), new PromptOptions { Prompt = MessageFactory.Text("Would you like to view your token?") }, cancellationToken);

我测试了将其添加到* DOES SOMETHING WHEN LOGGED IN*中的代码中,并且有效:

enter image description here