我正在使用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与机器人模板中的相同,没有任何变化。
在两个模拟器窗口中一个接一个地测试它时,我注意到第一个模拟器提示登录,而另一个模拟器窗口自动获取上下文,并且不提示进行身份验证:
在调试时,我发现 botauth 中的 context.UserData.TryGetValue($"{this.authProvider.Name}{ContextConstants.AuthResultKey}", out authResult)
代码始终返回值,之后任何用户都经过身份验证。
但是当我从 azure 表中的状态管理更改为使用机器人框架的旧状态管理时,我得到了预期的行为。
我到底错过了什么?是一些 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/