none
Authentication for a Bot using ASP.NET Core 2.0 targeting .NET Framework RRS feed

  • Question

  • I am trying to create a bot using the Bot Builder SDK using ASP.NET targeting .NET Framework.

    While testing it through the Bot Framework emulator, I getting a Microsoft.Bot.Connector.MicrosoftAppCredentials.OAuthException while invoking a dialog.

    I am attaching the code snippets for clarity.

    Startup.cs

    public class Startup
        {
            public Startup(IHostingEnvironment env)
            {
                var builder = new ConfigurationBuilder()
                    .SetBasePath(env.ContentRootPath)
                    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                    .AddEnvironmentVariables();
                Configuration = builder.Build();
            }
    
            public IConfiguration Configuration { get; }
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddSingleton(_ => Configuration);
    
                var credentialProvider = new StaticCredentialProvider(Configuration.GetSection(MicrosoftAppCredentials.MicrosoftAppIdKey)?.Value,
                   Configuration.GetSection(MicrosoftAppCredentials.MicrosoftAppPasswordKey)?.Value);
                // var credentialProvider = new StaticCredentialProvider("","");
    
    
                services.AddAuthentication(options =>
                {
                    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
                }).AddBotAuthentication(credentialProvider); ;
    
    
    
                services.AddSingleton(typeof(ICredentialProvider), credentialProvider);
                services.AddMvc();
                services.AddMvc(options =>
                {
                    options.Filters.Add(typeof(TrustServiceUrlAttribute));
                });
                Conversation.UpdateContainer(
                    builder =>
                    {
                        var store = new InMemoryDataStore();
                        builder.Register(c => store)
                                    .Keyed<IBotDataStore<BotData>>(new object())
                                    .AsSelf()
                                    .SingleInstance();
                        builder.Register(c => new MicrosoftAppCredentials(
                            MicrosoftAppCredentials.MicrosoftAppIdKey,
                            MicrosoftAppCredentials.MicrosoftAppPasswordKey
                        
                        )).SingleInstance();
                    });
            }
        
    
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                app.UseStaticFiles();
                app.UseAuthentication();
                app.UseMvc();
            }
        }

    MessagesController.cs

     [BotAuthentication]
        
        [Route("api/Messages")]
        
        public class MessagesController : Controller
        {
            private readonly IConfiguration configuration;
    
            public MessagesController(IConfiguration configuration)
            {
                this.configuration = configuration;
            }
            //static MessagesController()
            //{
    
            //}
            [Authorize(Roles = "Bot")]
            [HttpPost]
            public async Task<HttpResponseMessage> Post([FromBody] Activity activity)
            {
                var appCredentials = new MicrosoftAppCredentials(
                    configuration.GetSection(MicrosoftAppCredentials.MicrosoftAppIdKey)?.Value,
                    configuration.GetSection(MicrosoftAppCredentials.MicrosoftAppPasswordKey)?.Value);
                
                MicrosoftAppCredentials.TrustServiceUrl(activity.ServiceUrl, DateTime.MaxValue);
                
                var client = new ConnectorClient(new Uri(activity.ServiceUrl), appCredentials);
                var b = MicrosoftAppCredentials.IsTrustedServiceUrl(activity.ServiceUrl);
                //var reply = activity.CreateReply("Hi");
               // await client.Conversations.ReplyToActivityAsync(reply);
                try
                {
                    if (activity.Type == ActivityTypes.Message)
                    {
                        await Conversation.SendAsync(activity, MakeDialog);
                    }
                    else
                    {
    
                    }
                }
                catch (Exception e)
                {
    
                }
                return new HttpResponseMessage(System.Net.HttpStatusCode.Accepted);
            }
    
    
    
            private IDialog<string> MakeDialog()
            {
    
                return Chain.From(() => new RootDialog());
            }
        }


    It is throwing "Microsoft.Bot.Connector.MicrosoftAppCredentials.OAuthException" at Conversation.SendAsync().

    It would be great if someone could shed some light on what might be going wrong. Thanks.

    • Moved by CoolDadTx Tuesday, April 10, 2018 5:11 PM ASP.NET related
    Tuesday, April 10, 2018 2:35 PM

All replies

  • It would be great if someone could shed some light on what might be going wrong. Thanks.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/df14dc5f-982b-4676-b767-6123c8a90495/where-is-the-correct-place-to-make-posts-regarding-aspnet-mvc-webapi-and-core-sql-or-another?forum=csharpgeneral

    Tuesday, April 10, 2018 2:49 PM