用户在MS botframework中登录后如何获取身份验证令牌

时间:2019-01-08 18:04:29

标签: c# .net azure botframework

提出的问题:用户登录后如何获取身份验证令牌?

背景: 我正在使用Azure AD v1为我的机器人设置身份验证,连接设置如下所示。

enter image description here

当我测试连接时,我能够登录,然后返回连接令牌并说:“测试到'BotAuth'的连接成功”

Bot身份验证::当我尝试登录Bot时,系统提示我登录卡(如下所示),

enter image description here

,当您单击登录时,会弹出一个对话框来登录。登录后,对话框将关闭,并且没有其他任何反应。我在下面提供了身份验证代码以供参考。通过分析我的代码发现,登录后再也不会调用方法“ FinishLoginDialog”。从我阅读的内容来看,您应该在登录后获得身份验证令牌(很像测试连接),但是我从不这样做。那么回到我的问题,用户登录后如何获得身份验证令牌?

public class AuthenticationDialog : ComponentDialog
{
    private static AuthenticationResponses _responder = new AuthenticationResponses();

    public AuthenticationDialog()
        : base(nameof(AuthenticationDialog))
    {
        InitialDialogId = nameof(AuthenticationDialog);
        ConnectionName = "BotAuth";

        var authenticate = new WaterfallStep[]
        {
            PromptToLogin,
            FinishLoginDialog,
        };

        AddDialog(new WaterfallDialog(InitialDialogId, authenticate));
        AddDialog(new OAuthPrompt(DialogIds.LoginPrompt, new OAuthPromptSettings()
        {
            ConnectionName = ConnectionName,
            Title = AuthenticationStrings.TITLE,
            Text = AuthenticationStrings.PROMPT,
        }));
    }

    private string ConnectionName { get; set; }

    private async Task<DialogTurnResult> PromptToLogin(WaterfallStepContext sc, CancellationToken cancellationToken)
    {
        return await sc.PromptAsync(AuthenticationResponses.ResponseIds.LoginPrompt, new PromptOptions());
    }

    private async Task<DialogTurnResult> FinishLoginDialog(WaterfallStepContext sc, CancellationToken cancellationToken)
    {
        var activity = sc.Context.Activity;
        if (sc.Result != null)
        {
            var tokenResponse = sc.Result as TokenResponse;

            if (tokenResponse?.Token != null)
            {
                var user = await GetProfile(sc.Context, tokenResponse);
                await _responder.ReplyWith(sc.Context, AuthenticationResponses.ResponseIds.SucceededMessage, new { name = user.DisplayName });
                return await sc.EndDialogAsync(tokenResponse);
            }
        }
        else
        {
            await _responder.ReplyWith(sc.Context, AuthenticationResponses.ResponseIds.FailedMessage);
        }
        return await sc.EndDialogAsync();
    }

    private async Task<User> GetProfile(ITurnContext context, TokenResponse tokenResponse)
    {
        var token = tokenResponse;
        var client = new GraphClient(token.Token);

        return await client.GetMe();
    }

    private class DialogIds
    {
        public const string LoginPrompt = "loginPrompt";
    }
}

0 个答案:

没有答案