botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

Microsoft Bot: Bot Data getProperty is returning null when I try to set Property

Open msBot1111 opened this issue 9 years ago • 6 comments

I'm trying to authenticate my Bot using my web project and I store the token in Bot Data using Bot State. But when I try to get thee token from my Bot using getProperty it always returns null. I'm using this sample: https://blogs.msdn.microsoft.com/tsmatsuz/2016/09/06/microsoft-bot-framework-bot-with-authentication-and-signin-login/ Here is my code in web: var botCred = new MicrosoftAppCredentials( ConfigurationManager.AppSettings["MicrosoftAppId"], ConfigurationManager.AppSettings["MicrosoftAppPassword"]); var stateClient = new StateClient(botCred); BotState botState = new BotState(stateClient); BotData botData = new BotData(eTag: "*"); botData.SetProperty("AccessToken", authResult.AuthenticationToken); await stateClient.BotState.SetPrivateConversationDataAsync(TempData["channelid"].ToString(), TempData["conversationid"].ToString(), TempData["skypeuserid"].ToString(), botData); And here is in my Bot: ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl)); StateClient stateClient = activity.GetStateClient(); BotState botState = new BotState(stateClient); BotData botData = await botState.GetPrivateConversationDataAsync(activity.ChannelId,activity.Conversation.Id, activity.From.Id); string token = botData.GetProperty("AccessToken"); The token is always null.

msBot1111 avatar Jan 20 '17 13:01 msBot1111

It seems very likely that (TempData["channelid"].ToString(), TempData["conversationid"].ToString(), TempData["skypeuserid"].ToString()) does not match (activity.ChannelId,activity.Conversation.Id, activity.From.Id) - can you check the values?

willportnoy avatar Jan 20 '17 17:01 willportnoy

The values are the same..

msBot1111 avatar Jan 23 '17 08:01 msBot1111

Are the StateClient's ServiceURLs the same?

willportnoy avatar Jan 24 '17 16:01 willportnoy

No, they are not the same.. In web app I have: var botCred = new MicrosoftAppCredentials( ConfigurationManager.AppSettings["MicrosoftAppId"], ConfigurationManager.AppSettings["MicrosoftAppPassword"]); var stateClient = new StateClient(botCred); BotState botState = new BotState(stateClient); this has state.botframework.com And in Bot I use this: StateClient stateClient = activity.GetStateClient(); BotState botState = new BotState(stateClient); which has localhost url. But if I use the same code in Bot which is in the web app it gives 500 internal server error in this line BotData botData = await stateClient.BotState.GetPrivateConversationDataAsync(channel,conversation, id)

msBot1111 avatar Jan 25 '17 09:01 msBot1111

@msft-shahins I wonder if Activity.GetStateClient isn't propagating the MicrosoftAppCredentials?

willportnoy avatar Jan 25 '17 22:01 willportnoy

Code above is using this implementation of GetStateClient() which creates a new MicrosoftAppCredentials. Notice that it load the MicrosoftAppId and MicrosoftAppPassword from configuration manager since they are passed as null to the constructor: https://github.com/Microsoft/BotBuilder/blob/3a98a6b3d15962a57b5454bfb3f730d3588de3ef/CSharp/Library/Microsoft.Bot.Connector.Shared/MicrosoftAppCredentials.cs#L54

You need a valid channel.id, conversation.id, user.id to call GetPrivateConversationData. Here is an example: https://github.com/Microsoft/BotBuilder/blob/10893730134135dd4af4250277de8e1b980f81c9/CSharp/Tests/Microsoft.Bot.Builder.Tests/BotDataTests.cs#L172

Note that service url for the state service will be the serviceUrl from the activity when channel.id == emulator since emulator is acting as the state service during emulation. But state.botframework.com will be used for other channels: https://github.com/Microsoft/BotBuilder/blob/a71e64c24bd40f8b99de0a3326ea1b79110c33e1/CSharp/Library/Microsoft.Bot.Connector.Shared/ActivityEx.cs#L146

Closing this since we cannot repro the issue. Feel free to open another issue with the repro steps.

msft-shahins avatar Feb 08 '17 01:02 msft-shahins