MicrosoftDX / AuthBot

General authentication/authorization Bot Framework sample
MIT License
82 stars 54 forks source link

Unexpected behavior after implementing TableBotDataStore #28

Open waynehsmith opened 7 years ago

waynehsmith commented 7 years ago

Install Azure Storage Emulator and check operation

Steps to reproduce using SampleAADv2Bot:

  1. Install NuGet Package Autofac.WebApi2
  2. Install NuGet package Microsoft.BotBuilder.Azure

in Global.asax.cs

  1. Add the following after the Application Start method private void ConfigureBotTableStorage() { string tableName = "UserStateDev";

        TableBotDataStore store = new TableBotDataStore(CloudStorageAccount.DevelopmentStorageAccount, tableName);
    
        var builder = new ContainerBuilder();
    
        builder.Register(c => store)
            .Keyed<IBotDataStore<BotData>>(AzureModule.Key_DataStore)
            .AsSelf()
            .SingleInstance();
    
        builder
            .Register(c =>
            {
                return new CachingBotDataStore(store, CachingBotDataStoreConsistencyPolicy.ETagBasedConsistency);
            })
            .As<IBotDataStore<BotData>>()
            .AsSelf()
            .InstancePerLifetimeScope();
    
        // Get your HttpConfiguration.
        var config = GlobalConfiguration.Configuration;
    
        // Register your Web API controllers.
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
    
        // OPTIONAL: Register the Autofac filter provider.
        builder.RegisterWebApiFilterProvider(config);
    
        // Set the dependency resolver to be Autofac.
        //var container = builder.Build();
        builder.Update(Conversation.Container);
        config.DependencyResolver = new AutofacWebApiDependencyResolver(Conversation.Container);
      @@}
  2. Before the line GlobalConfiguration.Configure(WebApiConfig.Register); insert the line ConfigureBotTableStorage();

  3. Save and compile

  4. Connect to the bot on http://localhost:3978/api/messages with the Bot Framework Channel Emulator

  5. Send logon to the bot inthe emulator

  6. Click the Authentication Required link in the emulator

  7. Log in using a Microsoft account in the browser window that opens

  8. Copy the magic number to send back to the bot

Expected behavior

Bot prompts for the magic number

Actual behavior

Bot displays sign-in card again.

yellowwidget commented 7 years ago

Same issue. Did you figure out how to get past this problem?

shrubby commented 7 years ago

If someone has the answer to this please post.

Thanks!

waynehsmith commented 7 years ago

Afraid not.. I'm still experiencing the problem.

EricDahlvang commented 7 years ago

@weretygr @yellowwidget @shrubby

This is due to the fact that the current implementation of AuthBot is using the default state client in the OAuthCallbackController. Modifying the controller as follows will make it work with the TableBotDataStore implementation you are using:

1) add a new class that implements IAddress:

public class AddressKey : IAddress
{
    public string BotId { get; set; }
    public string ChannelId { get; set; }
    public string ConversationId { get; set; }
    public string ServiceUrl { get; set; }
    public string UserId { get; set; }
}

2) In place of IStateClient sc = scope.Resolve(); retrieve the IBotDataStore>

var botDataStore = scope.Resolve<IBotDataStore<BotData>>();`

3) Instead of sc.BotState.GetUserData use the botDataStore to retrieve the user BotData object:

var userData = await botDataStore.LoadAsync(key, BotStoreType.BotUserData, CancellationToken.None);

4) Instead of sc.BotState.SetUserData use the botDataStore to save the userData

await botDataStore.SaveAsync(key, BotStoreType.BotUserData, userData, CancellationToken.None);
await botDataStore.FlushAsync(key, CancellationToken.None);

I've created a PR for this: https://github.com/MicrosoftDX/AuthBot/pull/37

yellowwidget commented 7 years ago

@EricDahlvang - Thank you very much for your time investigating and figuring this out. I just tested and it works great.

vibjain commented 6 years ago

How do we fix this as we are using the AuthBot Nuget

EricDahlvang commented 6 years ago

@vibjain Please switch your project to using BotAuth. AuthBot uses the default state client, and will be deprecated in the future.

https://www.nuget.org/packages/BotAuth/3.9.0-alpha https://github.com/richdizz/BotAuth

vibjain commented 6 years ago

Can you provide some guidance on moving to the BotAuth, currently I am stuggling with the ContextExtensions methods like Logout & GetAccessToken will we have to implements these methods ourselves now? I don't see them in the BothAuth anywhere. Thanks

EricDahlvang commented 6 years ago

There is a little information on this page: https://github.com/richdizz/BotAuth/tree/7138ecf743423bcf13f53c09644ecdb60f0ec7da/CSharp and five sample applications. Have you reviewed those?

vibjain commented 6 years ago

Hi @EricDahlvang, thanks for your help. I think I made progress and changed my code form AuthBot to BotAuth. However after completing all the steps I am hitting this open issue. https://github.com/richdizz/BotAuth/issues/8 So, I cannot use the TableBotDataStore because AuthBot is not compatible & BotAuth has an open issue currently.

EricDahlvang commented 6 years ago

@vibjain I've created a PR to address this: https://github.com/richdizz/BotAuth/pull/10 Until it is merged, you can clone the repository, modify the local CallbackController.cs and include it in your project.

I hope this helps.