Search code examples
botframeworkazure-bot-servicedirect-line-botframework

Users have to sign in every time?


Right now, our users sign in every time they want to chat with our bot. It can be very annoying for them. Here's some information about the bot:

  • The bot is accessed using the Direct Line channel.
  • When I use the "Test in Web Chat" feature in Azure, I stay signed in every time. So it works fine there.
  • We are using the BotFramework-WebChat component for the UI.
  • I followed this tutorial to generate a user token. I generated a user ID like this:
    • var userId = "dl_" + new Random().Next() + new DateTime().Ticks;

Here is the code to authenticate users:

public GreetingDialog(IConfiguration configuration, IBotTelemetryClient telemetryClient)
    : base(INTENTS.GREETING, configuration["ConnectionName"])
{
    TelemetryClient = telemetryClient;

    AddDialog(new OAuthPrompt(
        nameof(OAuthPrompt),
        new OAuthPromptSettings
        {
            ConnectionName = ConnectionName,
            Text = "👋 Welcome! Please Sign In.",
            Title = "Sign In",
            Timeout = 30000,
        })
    {
        TelemetryClient = telemetryClient
    });

    AddDialog(new WaterfallDialog(INTENTS.GREETING, new WaterfallStep[] {
        PromptStepAsync,
        GreetStepAsync,
        })
    {
        TelemetryClient = telemetryClient
    });
    InitialDialogId = INTENTS.GREETING;
}

private async Task<DialogTurnResult> PromptStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
    return await stepContext.BeginDialogAsync(nameof(OAuthPrompt), null, cancellationToken);
}

private async Task<DialogTurnResult> GreetStepAsync(WaterfallStepContext step, CancellationToken cancellationToken)
{ 
    // ...
}

Solution

  • The problem is that every time you generate a user Id randomly, a new conversation is created. Being a new conversation, the bot cannot find the previous session.

    If you want to do a test you can assign a static ID user:

    var userId = "dl_123456789";

    It shouldn't ask you to log in again.

    To make a complete solution, the userId should be tied to some distinctive credentials in your application (ex: username/password)