c# - 在将 botauth 与 Azure 表存储结合使用时,登录的用户上下文也在新对话中使用

标签 c# azure-storage botframework

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

我的 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与机器人模板中的相同,没有任何变化。

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

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

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

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

最佳答案

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

注意:这在模拟器和网络聊天之外的 channel 上不会出现问题,因为用户的 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尚未发布到 nuget。默认状态客户端始终在当前 botauth 的 CallbackController 中检索。上下文对话框检索 autofac 状态客户端实现。上面的提交修复了这种不一致。它应该很快就会发布到 nuget。

关于c# - 在将 botauth 与 Azure 表存储结合使用时,登录的用户上下文也在新对话中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48073769/

相关文章:

c# - 内置比较叠加的 Visual Studio

c# - 绑定(bind)时已释放 ObjectContext 实例

iis - 剔除/管理 Blob 存储中的 Azure 日志文件/失败请求日志

Spring 无法 Autowiring Azure 存储 Blob

javascript - 如何访问 Microsoft Bot Framework 上的用户数据?

azure - 将 C# Teams 消息传递扩展 (Bot) 部署到 Azure 的其他步骤?

c# - 使用 AutoMapper 设置 NULL 更新现有域模型

c# - 不在 C# 中处理的随机对象

android - 使用 SAS(共享访问签名)通过 Azure 存储上传文件

c# - 如何将Adaptive Card填写的数据传回TestFlow和TestAdapter进行测试?