在将botauth与Azure表存储一起使用时,也记录了在新会话中使用的用户上下文

时间:2018-01-03 08:32:26

标签: c# azure-storage botframework

我正在使用BotAuth登录我的机器人用户。最近,我按照https://docs.microsoft.com/en-us/bot-framework/dotnet/bot-builder-dotnet-state-azure-table-storage中提到的步骤实施了Azure Table存储来存储和管理机器人的状态数据。

我的Global.asax.cs文件如下所示:

protected void Application_Start(object sender, EventArgs e)
    {
        {
            var config = GlobalConfiguration.Configuration;
            Conversation.UpdateContainer(
                builder =>
                {
                    builder.RegisterModule(new AzureModule(Assembly.GetExecutingAssembly()));

                    var store = new TableBotDataStore(ConfigurationManager.AppSettings["StorageConnectionString"], "botfactoryestimatorstate");
                    builder.Register(c => store)
                        .Keyed<IBotDataStore<BotData>>(AzureModule.Key_DataStore)
                        .AsSelf()
                        .SingleInstance();

                    builder.Register(c => new CachingBotDataStore(store,
                                 CachingBotDataStoreConsistencyPolicy.ETagBasedConsistency))
                         .As<IBotDataStore<BotData>>()
                         .AsSelf()
                         .InstancePerLifetimeScope();
                    // Register your Web API controllers.
                    builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
                    builder.RegisterWebApiFilterProvider(config);

                });

            config.DependencyResolver = new AutofacWebApiDependencyResolver(Conversation.Container);
        }

        // WebApiConfig stuff
        GlobalConfiguration.Configure(config =>
        {
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        });
    }

MessagesController 与机器人模板中的相同,没有任何变化。

在2个模拟器窗口中一个接一个地测试它时,我注意到第一个模拟器提示登录,而另一个模拟器窗口自动接收上下文,而不是提示进行身份验证: enter image description here

在调试它时,我发现在任何用户通过身份验证后,botauth中的context.UserData.TryGetValue($"{this.authProvider.Name}{ContextConstants.AuthResultKey}", out authResult)代码始终会返回值。

但是当我从azure表中的状态管理更改为使用bot框架的旧状态管理时,我得到了预期的行为。 enter image description here

我到底错过了什么?是一些autofac模块注册。有没有人有一个完美的工作样本。

1 个答案:

答案 0 :(得分:1)

这是因为模拟器的每个实例都拥有自己的状态服务。使用自定义状态客户端时,状态在所有连接的客户端之间共享(不使用模拟器的状态服务)。如果用户的id相同(就像在不同的仿真器实例中一样),则共享状态将检索相同的UserData(应该如此)。

注意:这不会是模拟器和网络聊天以外的其他渠道的问题,因为用户的ID不同。通过在BotChat启动时生成唯一的UserId,可以在网聊中克服这一点:

<script>
      var uniqueUserId = getUniqueUserId();
      BotChat.App({
        directLine: { secret: direct_line_secret },
        user: { id: uniqueUserId  },
        bot: { id: 'botid' },
        resize: 'detect'
      }, document.getElementById("bot"));
</script>

<击> https://github.com/MicrosoftDX/botauth/commit/b6e9751eab8d9b3bc02274882e4042ba788f4dca尚未发布。始终在当前botauth的CallbackController中检索默认状态客户端。上下文对话框检索autofac状态客户端实现。这种不一致是上述提交修复的内容。它应该很快发布到nuget。