2017-01-16 43 views
2

我怎麼能要求用戶從機器人登錄到我的網站,以及稍後當用戶要求bot首先檢查用戶是否在我的網站進行身份驗證,如果不是重定向到web登錄。我已經在bot中使用登錄卡進行了登錄,並且我傳遞了activity.user.id,但是如果用戶登錄或不登錄,我不知道如何檢索信息。微博殭屍框架,如何驗證用戶登錄網絡應用程序

ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl)); 
Activity reply = activity.CreateReply($"Well hello there. What can I do for you today?"); 
await connector.Conversations.ReplyToActivityAsync(reply); 

var id = activity.From.Id; 
reply.Attachments = new List<Attachment>(); 
List<CardAction> cardButtons = new List<CardAction>(); 
CardAction plButton = new CardAction() 
{ 
    Value = $"http://myapp.azurewebsites.net/Account/Login?userid='{id}'", 
    Type = "signin", 
    Title = "Connect" 
}; 

cardButtons.Add(plButton); 
SigninCard plCard = new SigninCard("You need to authorize me", new List<CardAction>() 
     { plButton }); 

Attachment plAttachment = plCard.ToAttachment(); 
reply.Attachments.Add(plAttachment); 
var replyt = await connector.Conversations.SendToConversationAsync(reply); 

回答

3

你可以通過這篇文章https://blogs.msdn.microsoft.com/tsmatsuz/2016/09/06/microsoft-bot-framework-bot-with-authentication-and-signin-login/
和樣品的GitHub代碼 https://github.com/tsmatsuz/AuthDemoBot

當你的web應用(網站)在瀏覽器中打開,頁面被重定向到登錄網址。 用戶登錄(登錄成功)後,您的Web應用程序可能會獲得一些經過身份驗證的安全令牌。使用Bot Framework API,您的Web應用程序將給定的令牌作爲用戶狀態存儲到機器人狀態服務中。 (此時,用戶標識用於標識符。) 用戶可以關閉您的Web應用程序(Web瀏覽器)。
最後,當用戶在您的機器人中輸入一些聊天時,您的機器人(在服務器端)可以從機器人狀態服務中檢索以前的令牌。 (從目前來看,這個機器人可以使用這個檢索令牌調用一些API)。

enter image description here

信用:剛鬆崎

+0

我試過這個,但是當我試圖讓機器人數據通過GetPrivateConversationDataAsync它總是返回null。 –

+0

檢查您實際設置令牌的位置。它是PrivateConversationData/UserData嗎? – Satheesh