代码之家  ›  专栏  ›  技术社区  ›  Alexandr Sulimov

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

  •  1
  • Alexandr Sulimov  · 技术社区  · 5 年前

    我有一个蓝色的a 使用AppId和AppPass enter image description here

    MicrosoftAppId MicrosoftAppPassword enter image description here

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

    enter image description here

    对于电报客户端,我也有同样的错误

    在“日志流”中,我看到 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 回复  |  直到 5 年前
        1
  •  4
  •   Drew Marsh    5 年前

    .bot files ! 您(正确地)向我们显示了通过应用程序设置正确配置您的机密的屏幕截图,但您的启动代码不依赖于应用程序设置。。。相反,它是在利用新技术 文件以加载终结点的凭据。

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

    例如,您可以将 只需将您的机器人注册更改为以下内容,即可归档并更改启动以使用应用程序设置:

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