新 Bot Framework 403 禁止 (.NET Core 2.1)

New Bot Framework 403 Forbidden (.NET Core 2.1)

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

我已经从 Visual Studio 部署了一个机器人到 App Service 并添加了 MicrosoftAppIdMicrosoftAppPassword

我尝试在 "Test in Web Chat" 中测试

并且有一个 403 禁止

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

在"Log stream"我明白了

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.");
           };
       });
    }

是什么原因?

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

首先让我说这是一项全新的可选技术。我知道样本往往会把它推到你面前,但如果你已经拥有 DevOps 实践,可以通过环境 variables/app 设置等现有机制很好地维护和部署你的 keys/secrets,你就不必采用它.

例如,您可以删除 .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 中使用的任何其他配置存储中加载它。