Search code examples
c#botframeworkmicrosoft-teams

How to properly logout from Teams BotFramework OauthPrompt?


I have a c# Teams bot that uses multiple Generic Oauth2 providers. As an example I have used this sample https://github.com/microsoft/BotBuilder-Samples/tree/main/samples/csharp_dotnetcore/46.teams-auth

Also I have added Generic OAuth2 to Bot Registration in Azure. Everything works fine with single Oauth provider.

User after logout process must be able to authenticate to another oauth2 provider. But I'm faced with problem that OauthPrompt caches previous credentials, and I don't know how to clear this cache and properly sign out user

I've already tried different approaches to sign out user:

  • Using UserTokenClient
var userTokenClient = innerDc.Context.TurnState.Get<UserTokenClient>();
await userTokenClient.SignOutUserAsync(innerDc.Context.Activity.From.Id, connectionName,
                            innerDc.Context.Activity.ChannelId, cancellationToken).ConfigureAwait(false)

  • Using OauthPrompt dialog
var oauth = (OAuthPrompt)Dialogs.Find(nameof(OAuthPrompt));
if (oauth != null)
{
    await oauth.SignOutUserAsync(innerDc.Context, cancellationToken: cancellationToken);
}

--Using BotFrameworkAdapter

await botAdapter.SignOutUserAsync(innerDc.Context, connectionName, cancellationToken: cancellationToken);

Please help


Solution

  • Found a problem. Bot was registered as a Singleton in services (services.AddSingleton <..>), аnd OauthPrompt dialog was added in waterfall step and didn't disposed. I have created different OauthPrompts for each Generic OAuth and added to dialog throught dialog constructor. And based user prompt choosed the right OauthPrompt

                AddDialog(new OAuthPrompt("1",
                    new OAuthPromptSettings
                    {
                        ConnectionName = "1",
                        Text = "Please Sign In",
                        Title = "Sign In",
                        Timeout = 300000, // User has 5 minutes to login (1000 * 60 * 5)
                    }));
                AddDialog(new OAuthPrompt("2",
                    new OAuthPromptSettings
                    {
                        ConnectionName = "2",
                        Text = "Please Sign In",
                        Title = "Sign In",
                        Timeout = 300000, // User has 5 minutes to login (1000 * 60 * 5)
                    }));