We have built a Teams bot using Bot Framework (version 4.0). The bot is working fine.
What we want to accomplish is provide a logout/login functionality in the bot. To logout, the user would type in a special command (say "log me out") and we would log the user out. Now when the user interacts with the bot next time, we want the user to manually sign-in by showing them the OAuth dialog.
This is the code we have written for signing out the user and it does not throw any errors:
var userTokenClient = innerDc.Context.TurnState.Get<UserTokenClient>();
await userTokenClient.SignOutUserAsync(innerDc.Context.Activity.From.Id, _connectionName, innerDc.Context.Activity.ChannelId, cancellationToken).ConfigureAwait(false);
await innerDc.Context.SendActivityAsync(MessageFactory.Text("You have been signed out."), cancellationToken);
return await innerDc.CancelAllDialogsAsync(cancellationToken);
However when the user sends the next message, Bot framework somehow automatically signs them in (I am guessing that the login session is terminated on the server but the tokens are not removed from the local cache).
I am wondering if there is another way to completely sign out the user and remove the token cache so that user is presented with an OAuth login dialog on the next conversation.
Someone suggested to remove Teams cache folder locally but that is also not working.
We even tried the following code and getting the same result:
await MyOAuthPrompt.SignOutUserAsync(innerDc.Context);
There are multiple ways to implement the bot authentication.
1) Auth using SSO (Single-Sign-On)
Reference doc: https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-authentication?view=azure-bot-service-4.0&tabs=userassigned%2Caadv2%2Ccsharp
You can refer this sample: https://github.com/OfficeDev/Microsoft-Teams-Samples/tree/main/samples/app-sso
2) Auth using OAuthCard
Reference doc link: https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-authentication-sso?view=azure-bot-service-4.0&tabs=eml
You can refer this sample: https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-authentication?view=azure-bot-service-4.0&tabs=userassigned%2Caadv2%2Ccsharp#prerequisites
Add authentication to a bot in Bot Framework SDK - Bot Service Learn how to add user's authentication to your bot using Azure authentication.