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.
.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 ?
I'm hitting this one too.
There is a sample here for AspNetCore2 with .Net framework. Let me know if that helps you at all.
My app is using Dialogs. Dialogs assume that _MicrosoftAppCredentials_ is registered correct in the _AutoFac_ container. Injecting it in api/messages Controller like it is done in the AspNetCore2 sample is not possible. At minimum I have no idea how to manage it. Here my Message controll function.
public async Task<OkResult> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
await Conversation.SendAsync(activity, () => { return new RootLuisDialog(); });
}
else
{
this.HandleSystemMessage(activity);
}
return Ok();
}
Dialogs are not supported in AspNetCore because there is no official Bot.Builder package that supports it currently.
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();
});
Well, in that case, to answer your original question any solution you come up with is the correct solution to your problem. That's awesome you found a way to get around this!
@ChristianRiedl did yout put this re-registration on Dialog or Controller ? I'm trying to publish my .net core bot, locally it runs nice, but when on Azure, it doesn't answert the chat
I have put it in the startup.cs once and for all... ;)
@EPinci did it work on Azure ?
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);
}
nice @ChristianRiedl I'll try it, my only issue is that when I publish my bot on Azure, the channel doesn't comunicate with it, then the bot doesn't work :(
Hi, @luccasmf
I ran into the same problem. Did you find a solution for this?
Local - my bot works good. But, when a deploy project on ubuntu, getting error on
await Conversation.SendAsync(activity, () => dialog, token)
Trace:
Microsoft.Bot.Connector.ErrorResponseException: Operation returned an invalid status code 'MethodNotAllowed’
at Microsoft.Bot.Connector.BotState.d__8.MoveNext()
@SapiPapik The issue you're seeing is because your bot needs to implement a custom state client. The default state service has been deprecated for new bots. More information can be found here: https://blog.botframework.com/2017/12/19/bot-state-service-will-soon-retired-march-31st-2018/
Thank you for opening an issue against the Bot Framework SDK v3. As part of the Bot Framework v4 release, we’ve moved all v3 work to a new repo located at https://github.com/microsoft/botbuilder-v3. We will continue to support and offer maintenance updates to v3 via this new repo.
From now on, https://github.com/microsoft/botbuilder repo will be used as hub, with pointers to all the different SDK languages, tools and samples repos.
As part of this restructuring, we are closing all tickets in this repo.
For defects or feature requests, please create a new issue in the new Bot Framework v3 repo found here:
https://github.com/microsoft/botbuilder-v3/issues
For Azure Bot Service Channel specific defects or feature requests (e.g. Facebook, Twilio, Teams, Slack, etc.), please create a new issue in the new Bot Framework Channel repo found here:
https://github.com/microsoft/botframework-services/issues
For product behavior, how-to, or general understanding questions, please use Stackoverflow.
https://stackoverflow.com/search?q=bot+framework
Thank you.
The Bot Framework Team
Most helpful comment
With my workaround, reregistering _MicrosoftAppCredentials_ in the _AutoFac_ container, dialogs and forms are working as expected.