【问题标题】:New Bot Framework 403 Forbidden (.NET Core 2.1)新的 Bot Framework 403 禁止 (.NET Core 2.1)
【发布时间】:2019-01-14 07:42:34
【问题描述】:

我在 Azure 中有一个带有 AppId 和 AppPass 的 Bot Channels Registration

我已从 Visual Studio 将 Bot 部署到 App Service 并添加 MicrosoftAppIdMicrosoftAppPassword

我在“网络聊天测试”中尝试测试

并且有一个 403 Forbidden

使用电报客户端我有同样的错误 POST 到 xxx 失败:POST 到机器人的端点失败,HTTP 状态为 403

在“日志流”中我看到

Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddBot<IAssistantBot>(options =>
        {
           var secretKey = Configuration.GetSection("botFileSecret")?.Value;

            // Loads .bot configuration file and adds a singleton that your Bot can access through dependency injection.
            var botConfig = BotConfiguration.Load(@".\IAssistant.Bot.bot", secretKey);
            services.AddSingleton(sp => botConfig);

            // Retrieve current endpoint.
            var service = botConfig.Services.Where(s => s.Type == "endpoint" && s.Name == "development").FirstOrDefault();
            if (!(service is EndpointService endpointService))
            {
               throw new InvalidOperationException($"The .bot file does not contain a development endpoint.");
            }

           options.CredentialProvider = new SimpleCredentialProvider(endpointService.AppId, endpointService.AppPassword);

            // Catches any errors that occur during a conversation turn and logs them.
            options.OnTurnError = async (context, exception) =>
           {
               await context.SendActivityAsync("Sorry, it looks like something went wrong.");
           };
       });
    }

什么原因?

【问题讨论】:

  • .NET 还是 Node?你能分享你的初始化逻辑吗?需要看看你是如何配置凭据的。
  • @DrewMarsh 感谢您的回答。我已经添加了一个有问题的细节。项目是 .NET Core 2.1
  • @DrewMarsh 感谢您的提示。错误出现在“当前端点”
  • 所以你明白了吗?只需为您的生产端点配置正确的凭据即可?
  • @DrewMarsh 在制作中我使用了具有错误凭据的开发端点

标签: configuration botframework credentials


【解决方案1】:

好的,欢迎来到 V4 和.bot files!您(正确地)向我们展示了通过应用程序设置正确配置您的秘密的屏幕截图,但您的启动代码不依赖于应用程序设置......而是利用新的 .bot 文件加载端点的凭据。

首先让我说这是一种全新的可选技术。我知道这些示例往往会将其推向您的面前,但如果您已经拥有 DevOps 实践,可以通过环境变量/应用程序设置等现有机制很好地维护和部署您的密钥/秘密,那么您不必采用它。

例如,您可以删除 .bot 文件并更改您的启动以使用应用程序设置,只需将您的机器人注册更改为:

public class Startup
{
    private readonly IConfiguration _configuration;

    public Startup(IConfiguration configuration)
    {
        // Ask for the configuration service to be injected so you can access config values (standard .NET Core 101 stuff)
        _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddBot<IAssistantBot>(options =>
        {
            // Load the values right out of configuration
            options.CredentialProvider = new SimpleCredentialProvider(
               _configuration.GetSection("MicrosoftAppId").Value,
               _configuration.GetSection("MicrosoftAppPassword").Value);

            // Catches any errors that occur during a conversation turn and logs them.
            options.OnTurnError = async (context, exception) =>
           {
               await context.SendActivityAsync("Sorry, it looks like something went wrong.");
           };
       });
    }
}

如您所见,对于初学者来说,它的代码要少得多,并且只利用了 .NET Core 提供的现有配置系统。您可以从 appsettings.json 文件、环境变量以及您可能习惯在 .NET Core 中使用的任何其他配置存储中加载它。

【讨论】:

  • 对于其他人,请注意 .bot 文件在 Bot SDK v4.3 中已弃用。此外,请确保您的 Azure 资源可以相互通信。我花了一个小时进行故障排除,因为我在我的应用服务上设置了一个 IP 地址访问限制,阻止了其他资源与之通信。
猜你喜欢
  • 2020-12-05
  • 2018-12-06
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 2019-02-23
  • 2019-11-29
  • 2020-12-29
  • 2020-12-01
相关资源
最近更新 更多