3
votes

how can I ask user to login to my web from bot, and later when user ask bot something to check first if user is authenticated in my web, if not to redirect to web to login. I've made the login using sign-in card in bot and I pass the activity.user.id, but I don't know how to retrieve the information if the user is logged in or not either.

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);
1

1 Answers

4
votes

You can go through this article https://tsmatz.wordpress.com/2016/09/06/microsoft-bot-framework-bot-with-authentication-and-signin-login/
and sample github code https://github.com/tsmatsuz/AuthDemoBot

when your web application (web site) is opened in the browser, the page is redirected to the login url. After the user log-in (login succeeded), your web application might get some authenticated security token. Using Bot Framework api, your web application stores the given token as user state into the bot state service. (At this time, the user id is used for identifier.) The user can close your web application (web browser).
Finally, when the user inputs some chat in your bot, your bot (in server side) can retrieve the previous token from bot state service. (From now, this bot can call some api using this retrieved token.)

enter image description here

Credit: Tsuyoshi Matsuzaki