Search code examples
c#azure-storagebotframework

Error on BotAuth with Azure Table Storage


I'm using BotAuth nuget package to login users on my bot. Recently I implemented Azure Table storage to store and manage bot’s state data, by following the steps mentioned in https://learn.microsoft.com/en-us/bot-framework/dotnet/bot-builder-dotnet-state-azure-table-storage.

My Global.asax.cs file looks like this :

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {

        var store = new TableBotDataStore(CloudStorageAccount.DevelopmentStorageAccount);
        Conversation.UpdateContainer(builder =>
        {

            builder.Register(c => store)
                .Keyed<IBotDataStore<BotData>>(AzureModule.Key_DataStore)
                .AsSelf()
                .SingleInstance();

            builder.Register(c => new CachingBotDataStore(store,
                      CachingBotDataStoreConsistencyPolicy
                      .ETagBasedConsistency))
                      .As<IBotDataStore<BotData>>()
                      .AsSelf()
                      .InstancePerLifetimeScope();
        });
        GlobalConfiguration.Configure(WebApiConfig.Register);
    }
}

And MessagesController is same as the one in the bot template:

[BotAuthentication]
public class MessagesController : ApiController
{
    /// <summary>
    /// POST: api/Messages
    /// Receive a message from a user and reply to it
    /// </summary>
    public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {
        if (activity.Type == ActivityTypes.Message)
        {
            await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
        }
        else
        {
            HandleSystemMessage(activity);
        }
        var response = Request.CreateResponse(HttpStatusCode.OK);
        return response;
    }

    private Activity HandleSystemMessage(Activity message)
    { ...... }
}

Now, on testing it out, I get the sign-in card as expected and after clicking and completing the authorization process, i get the following error in the browser :

{
"message": "An error has occurred.",
"exceptionMessage": "Object reference not set to an instance of an object.",
"exceptionType": "System.NullReferenceException",
"stackTrace": "   at BotAuth.AADv1.ADALAuthProvider.<GetTokenByAuthCodeAsync>d__4.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at BotAuth.Controllers.CallbackController.<Callback>d__3.MoveNext()"
}

What exactly am I missing? Is it some autofac module registration. Does anyone have any working sample for this.


Solution

  • As pointed out by @FeiHan in the comments, this is a open issue in richdizz/BotAuth. - "Enabling custom state service causes authentication failure".

    This issue has been fixed in MicrosoftDX/botauth, hence preferred to use this one instead