Bot-docs: Authentication part is unclear

Created on 8 Jan 2019  Â·  7Comments  Â·  Source: MicrosoftDocs/bot-docs

  1. It is unclear how authentication settings should be added to the bot configuration (it looks like https://github.com/Microsoft/AI/blob/master/templates/Enterprise-Template/src/csharp/EnterpriseBotSample/BotServices.cs#L128-L138 expects the configuration in the .bot file).
  2. It looks like the authentication settings expect an Azure Active Directory v2 configuration? Can it be used with Azure Active Directory v1?
  3. How will this work locally in the emulator?

Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

platform SDK v4 VA

Most helpful comment

It is unclear how the token response should be handled in the Enterprise Bot Template.

When follow the steps in https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-enterprise-template-deployment?view=azure-bot-service-4.0#authentication:

  • I add AddDialog(new AuthenticationDialog(_services.AuthConnectionName)); to the constructor of MainDialog
  • I add var authResult = await dc.BeginDialogAsync(nameof(AuthenticationDialog)); as the last statement in the OnStartAsync method of MainDialog

then I get the question to log in, but the FinishLoginDialog of AuthenticationDialog is never called. Instead the token response ends up in OnEventAsync of MainDialog.

I inserted

    if (dc.Context.Activity.Name == "tokens/response")
    {
        var result = await dc.ContinueDialogAsync();
        return;
    }
    // Check if there was an action submitted from intro card
    else

ahead of https://github.com/Microsoft/AI/blob/440ef2eaa8b2dcff7d51fd7d169c7c1d7389e799/templates/Enterprise-Template/src/csharp/EnterpriseBotSample/Dialogs/Main/MainDialog.cs#L146, but I'm not sure whether that is the correct way to solve it.

How is this AuthenticationDialog supposed to be used?

All 7 comments

Some answers I found myself:

  • I found the needed command for 1. in https://github.com/Microsoft/botbuilder-tools/issues/713 but that was really not the place where I expected to find it.
  • For 2. it seems that Azure Active Directory v2 is just a key name where you can also put another the connection name of another authentication type in. A bit confusing.
  • For 3. it seems that it just works with the emulator when you use the msbot add command. More documentation on this would be welcome.

It is unclear how the token response should be handled in the Enterprise Bot Template.

When follow the steps in https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-enterprise-template-deployment?view=azure-bot-service-4.0#authentication:

  • I add AddDialog(new AuthenticationDialog(_services.AuthConnectionName)); to the constructor of MainDialog
  • I add var authResult = await dc.BeginDialogAsync(nameof(AuthenticationDialog)); as the last statement in the OnStartAsync method of MainDialog

then I get the question to log in, but the FinishLoginDialog of AuthenticationDialog is never called. Instead the token response ends up in OnEventAsync of MainDialog.

I inserted

    if (dc.Context.Activity.Name == "tokens/response")
    {
        var result = await dc.ContinueDialogAsync();
        return;
    }
    // Check if there was an action submitted from intro card
    else

ahead of https://github.com/Microsoft/AI/blob/440ef2eaa8b2dcff7d51fd7d169c7c1d7389e799/templates/Enterprise-Template/src/csharp/EnterpriseBotSample/Dialogs/Main/MainDialog.cs#L146, but I'm not sure whether that is the correct way to solve it.

How is this AuthenticationDialog supposed to be used?

@hansmbakker: have you figured out how to properly use AuthenticationDialog? i have the same problem here. can't find any solid solution to this one for days already. thanks

I don't know if it is the correct way, so I would really appreciate somebody from the bot builder team having a look at this.

@cleemullins @johnataylor since you work on the Bot Framework, could you help out here or could you suggest who can give us confirmation that this is the way to go?

I did the following:

  • In MainDialog, update OnEventAsync(…) to :
protected override async Task OnEventAsync(DialogContext dc, CancellationToken cancellationToken = default(CancellationToken))
{
    if (dc.Context.Activity.Name == "tokens/response")
    {
        var result = await dc.ContinueDialogAsync();
        return;
    }
    // Check if there was an action submitted from intro card
    else if (dc.Context.Activity.Value != null)
    {
        dynamic value = dc.Context.Activity.Value;
        if (value.action == "startOnboarding")
        {
            await dc.BeginDialogAsync(nameof(OnboardingDialog));
            return;
        }
        //else if(value)
    }
}
  • In the dialog where you want to use authentication, do the following:

    • add this to the constructor: AddDialog(new AuthenticationDialog(_botServices.AuthConnectionName));

    • add the following code as the first WaterfallStep:

public async Task<DialogTurnResult> CheckLogin(WaterfallStepContext sc, CancellationToken cancellationToken)
{
    var adapter = sc.Context.Adapter as BotFrameworkAdapter;
    var tokenResponse = await adapter.GetUserTokenAsync(sc.Context, _botServices.AuthConnectionName, null, cancellationToken);
    if (tokenResponse is null)
    {
        await _responder.ReplyWith(sc.Context, CreateProjectResponses.ResponseIds.ShouldLoginMessage);
        // If Bot Service does not have a token, send an OAuth card to sign in
        var authResult = await sc.BeginDialogAsync(nameof(AuthenticationDialog));
        return authResult;
    }
    else
    {
        return await sc.NextAsync(tokenResponse);
    }
}
  • use the tokenResponse in the next WaterfallStep:
TokenResponse tokenResponse = null;
if(sc.Result is TokenResponse)
{
    tokenResponse = sc.Result as TokenResponse;
}

var user = await GetProfile(sc.Context, tokenResponse);

I think your solution here at least gives light to how its supposed to be used. @hansmbakker thank you

any updates? @hansmbakker @miojoffey

We've added this topic on auth that link to additional content for v4.

Was this page helpful?
0 / 5 - 0 ratings