簡體   English   中英

Microsoft Teams bot - 鏈接展開身份驗證流程

[英]Microsoft Teams bot - link unfurling auth flow

找不到用於鏈接展開的身份驗證流程的好示例。 我設法使用此示例運行 oauth 流程。 但是在用戶提供登錄名和密碼並且機器人第二次點擊OnTeamsAppBasedLinkQueryAsync后, GetUserTokenAsync仍然返回 null。 因此,當身份驗證流程完成時,我不遵循我應該從哪里獲取令牌。 我應該以某種方式堅持下去嗎? Teams 會在每次請求時向我發送令牌嗎?它應該如何工作?

因此,在我的情況下,以下代碼始終返回 null:

var tokenResponse = await (turnContext.Adapter as IUserTokenProvider)
    .GetUserTokenAsync(turnContext, _connectionName, default(string),
        cancellationToken: cancellationToken);

AppBasedLinkQuery上似乎不存在“狀態”字段。 當驗證流程完成, OnTeamsAppBasedLinkQueryAsync將會再次調用和turnContext.Activity.Value將包含URL和“國家”(或魔碼)。 我們將把這個字段添加到AppBasedLinkQuery (在這里創建了一個問題: microsoft/botbuilder-dotnet#3429 )。

一種解決方法是直接從Activity.Value檢索狀態/magiccode 類似於:

 protected async override Task<MessagingExtensionResponse> OnTeamsAppBasedLinkQueryAsync(ITurnContext<IInvokeActivity> turnContext, AppBasedLinkQuery query, CancellationToken cancellationToken)
        {
            var magicCode = string.Empty;
            var state = (turnContext.Activity.Value as Newtonsoft.Json.Linq.JObject).Value<string>("state");
            if (!string.IsNullOrEmpty(state))
            {
                int parsed = 0;
                if (int.TryParse(state, out parsed))
                {
                    magicCode = parsed.ToString();
                }
            }

            var tokenResponse = await(turnContext.Adapter as IUserTokenProvider).GetUserTokenAsync(turnContext, _connectionName, magicCode, cancellationToken: cancellationToken);
            if (tokenResponse == null || string.IsNullOrEmpty(tokenResponse.Token))
            {
                // There is no token, so the user has not signed in yet.

                // Retrieve the OAuth Sign in Link to use in the MessagingExtensionResult Suggested Actions
                var signInLink = await(turnContext.Adapter as IUserTokenProvider).GetOauthSignInLinkAsync(turnContext, _connectionName, cancellationToken);

                return new MessagingExtensionResponse
                {
                    ComposeExtension = new MessagingExtensionResult
                    {
                        Type = "auth",
                        SuggestedActions = new MessagingExtensionSuggestedAction
                        {
                            Actions = new List<CardAction>
                                {
                                    new CardAction
                                    {
                                        Type = ActionTypes.OpenUrl,
                                        Value = signInLink,
                                        Title = "Bot Service OAuth",
                                    },
                                },
                        },
                    },
                };
            }

            var heroCard = new ThumbnailCard
            {
                Title = "Thumbnail Card",
                Text = query.Url,
                Images = new List<CardImage> { new CardImage("https://raw.githubusercontent.com/microsoft/botframework-sdk/master/icon.png") },
            };

            var attachments = new MessagingExtensionAttachment(HeroCard.ContentType, null, heroCard);
            var result = new MessagingExtensionResult("list", "result", new[] { attachments });

            return new MessagingExtensionResponse(result);
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM