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

时间:2019-01-12 15:06:46

标签: configuration botframework credentials

我在Azure中使用AppId和AppPass进行了 Bot Channels注册 enter image description here

我已将Bot从Visual Studio部署到App Service,并添加了 MicrosoftAppId MicrosoftAppPassword enter image description here

我尝试在“网络聊天测试”中进行测试 enter image description here

并禁止403 enter image description here

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

在“日志流”中,我看到了 enter image description here

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

是什么原因?

1 个答案:

答案 0 :(得分:3)

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

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

例如,您可以将.bot文件剪切掉,并通过将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中使用的其他配置存储中加载它。