question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[.NET Core] AspNetCore 2 app using .NET framework does not initialize MicrosoftAppCredentials

See original GitHub issue

I implemented a AspNetCore 2 Bot using .Net 462. I get a notauthorized exception when an intent message is processed by LuisDialog because MicrosoftAppCredentials is not initialized with the credentials.

DialogModule.Load registers MicrosoftAppCredentials without specifying parameters.

builder
       .RegisterType<MicrosoftAppCredentials>()
       .AsSelf()
       .SingleInstance();

When MicrosoftAppCredentials is created this constructor is used with both appId, password set to null :

        public MicrosoftAppCredentials(string appId = null, string password = null)
        {
            MicrosoftAppId = appId;
            MicrosoftAppPassword = password;
            TokenCacheKey = $"{MicrosoftAppId}-cache";
        }

I found a workaround, reregistering MicrosoftAppCredentials :

Conversation.UpdateContainer(
      builder =>
      {
             builder.Register(c => new MicrosoftAppCredentials(microsoftAppId, microsoftAppPassword)).SingleInstance();
      }); 

What is the correct solution for my problem ?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
ChristianRiedlcommented, Dec 22, 2017

With my workaround, reregistering MicrosoftAppCredentials in the AutoFac container, dialogs and forms are working as expected.

Conversation.UpdateContainer(
      builder =>
      {
             builder.Register(c => new MicrosoftAppCredentials(microsoftAppId, microsoftAppPassword)).SingleInstance();
      }); 
1reaction
ChristianRiedlcommented, Jan 3, 2018

Here my Code in Startup.cs. I did not test it in Azure.

public void ConfigureServices(IServiceCollection services)
        {
            var loggerFactory = services.BuildServiceProvider().GetRequiredService<ILoggerFactory>() as ILoggerFactoryEx;
            loggerFactory.AddProviderLastErrorOfThread(120, LogLevel.Warning);
            ILoggerEx logger = loggerFactory.CreateLogger("HomeBot") as ILoggerEx;
            services.AddSingleton<ILoggerEx>(logger);

            services.AddSingleton<IConfiguration>(Configuration);
            string microsoftAppId = Configuration[MicrosoftAppCredentials.MicrosoftAppIdKey];
            string microsoftAppPassword = Configuration[MicrosoftAppCredentials.MicrosoftAppPasswordKey];

            services.AddAuthentication().AddBotAuthentication(microsoftAppId, microsoftAppPassword);

            Conversation.UpdateContainer(
                builder =>
                {
                    var store = new InMemoryDataStore();
                    builder.Register(c => store)
                                .Keyed<IBotDataStore<BotData>>(new object())
                                .AsSelf()
                                .SingleInstance();
                    builder.Register(c => new MicrosoftAppCredentials(microsoftAppId, microsoftAppPassword)).SingleInstance();
                });


            services.AddMvc(options =>
            {
                options.Filters.Add(typeof(TrustServiceUrlAttribute));
            });

            var oauthConfig = new OAuthConfiguration("smarthome", null, "smarthome_service", "C4EEED8E-3171-4C9B-B632-614B2C09BC7F", "smarthome", null);
            var devicesProxy = new DevicesProxy("http://www.christian-riedl.com/smarthome/apismarthome", false, logger, new OAuthTokenClient(oauthConfig), true);
            services.AddSingleton(devicesProxy);

        }
Read more comments on GitHub >

github_iconTop Results From Across the Web

[.NET Core] AspNetCore 2 app using .NET framework does ...
I implemented a AspNetCore 2 Bot using .Net 462. ... MicrosoftAppCredentials is not initialized with the credentials. DialogModule.Load registers ...
Read more >
.NET Framework initialization errors: Managing the user ...
This situation typically occurs when an application requires a CLR version that is invalid or not installed on a given computer. If the ......
Read more >
Troubleshooting 'This application could not be started'
Error causes. This error typically indicates one of the following conditions: A .NET Framework installation on your system has become corrupted.
Read more >
Port from .NET Framework to .NET 6 - .NET Core
Understand the porting process and discover tools you may find helpful when porting a .NET Framework project to .NET 6.
Read more >
Migrate from ASP.NET to ASP.NET Core | Microsoft Learn
This article serves as a reference guide for migrating ASP.NET apps to ASP.NET Core. Visual Studio has tooling to help migrate ASP.NET apps...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found