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

500 Internal Server Error in emulator - V3

Open Huskitch opened this issue 10 years ago • 54 comments

I've recently upgraded my bot to the new V3 bot framework, and since then I've been getting 500 InternalServerErrors in the emulator and the "sorry, the bot is having trouble responding" messages in Slack, breakpoints anywhere in the method below don't get triggered at all so I can't really get a stack trace.

        public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
        {
            if (activity.Type == ActivityTypes.Message)
            {
                ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

                // return our reply to the user
                Activity reply = activity.CreateReply("hello world");
                await connector.Conversations.ReplyToActivityAsync(reply);
            }
            else
            {
                HandleSystemMessage(activity);
            }
            var response = Request.CreateResponse(HttpStatusCode.OK);
            return response;
        }

This code is the default VS template code, with the correct Web.config settings.

Below is what appears in the log when testing the connection to the bot:

InternalServerError
{
  "message": "An error has occurred.",
  "exceptionMessage": "Could not load type 'System.IdentityModel.Tokens.TokenValidationParameters' from assembly 'System.IdentityModel.Tokens.Jwt, Version=5.0.0.127, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.",
  "exceptionType": "System.TypeLoadException",
  "stackTrace": "   at Microsoft.Bot.Connector.BotAuthentication.<OnAuthorizationAsync>d__16.MoveNext()\r\n   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine)\r\n   at Microsoft.Bot.Connector.BotAuthentication.OnAuthorizationAsync(HttpActionContext actionContext, CancellationToken cancellationToken)\r\n   at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.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.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"
}

Huskitch avatar Jul 12 '16 07:07 Huskitch

There is something really strange going on. When you load the Bot Template it installs the Package System.IdentityModel.Tokens.Jwt with the version 4.02. When i try it with that version i get the exception 402: Unauthorized with: Warning: 0 : Invalid token. System.ArgumentNullException: Value cannot be null.

When iupdate that package to the latest version (5.0.0) i get the same exception as Huskitch. Well, i only get a 500 InternalServerError exception with the message "An error has occured".

Does it have something to do with the BotID in the web.config? Because on the page about the emulator, you see screenshots where you can enter a botid. I downloaded the latest version but i cant enter a botid.

Greetings

NanashiGIT avatar Jul 12 '16 10:07 NanashiGIT

Okay. Im pretty sure its because of your "System.IdentityModel.Tokens.Jwt" Version. Mine was also at 5.0.0. which for some reason broke everything.

I converted everything back to the latest 4.02. Version and it kind of works. I just get the Message: "iisexpress.exe Warning: 0 : Invalid token. System.IdentityModel.Tokens.SecurityTokenSignatureKeyNotFoundException: IDX10500: Signature validation failed. Unable to resolve SecurityKeyIdentifier: 'SecurityKeyIdentifier" everytime my bot receives a message, but it still works.

Im pretty sure there is something wrong with the Botcredentials in the web.config and what the Emulator/BotConnector expects.

Greetings

NanashiGIT avatar Jul 12 '16 12:07 NanashiGIT

I tried downgrading the JWT package to 4.02, and now I end up with this in the JSON emulator output

image

Huskitch avatar Jul 12 '16 12:07 Huskitch

Had the same issue and couldnt resolve it. Thankfully i had a backup of my project from before the V3 update. Im pretty sure some references are totally messed up. Maybe try to delete the package and reinstall. but i tried that too and it didnt work out.

NanashiGIT avatar Jul 12 '16 12:07 NanashiGIT

Hi, I'm experiencing similar issues... Even the sample Echo Bot cloned directly from the repo doesn't work. Apparently it doesn't receive proper Authorization headers in BotAuthentication.cs -> OnAuthorizationAsync().

lamaq-git avatar Jul 12 '16 12:07 lamaq-git

Hi. Try to reset the project references.

  1. remove all references of Bot.Builder and Bo.Connector (and itself)
  2. add via nuget Bot.Builder 3.0.1 and WebHost
  3. not update anything in Nuget
  4. adjust manually the assemblyBinding in terms of versios to reflect your project

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.2.29.0" newVersion="4.2.29.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.IdentityModel.Tokens.Jwt" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.0.20622.1351" newVersion="4.0.20622.1351" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Microsoft.Rest.ClientRuntime" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-1.0.0.0" newVersion="1.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Microsoft.Bot.Connector" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Microsoft.IdentityModel.Protocol.Extensions" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-1.0.2.33" newVersion="1.0.2.33" /> </dependentAssembly> </assemblyBinding>

rmatosaw avatar Jul 12 '16 13:07 rmatosaw

Even if this worked (which it didn't for me), it seems like a temporary solution to the issue.

Huskitch avatar Jul 12 '16 13:07 Huskitch

I uninstalled packages from NuGet, deleted "bin", "obj" and "packages", re-added everything and now the 500 error is gone, but the 401 still persists:

iisexpress.exe Warning: 0 : Invalid token. System.ArgumentNullException: Value cannot be null.
Parameter name: securityToken
   at System.IdentityModel.Tokens.JwtSecurityTokenHandler.ValidateToken(String securityToken, TokenValidationParameters validationParameters, SecurityToken& validatedToken) in c:\workspace\WilsonForDotNet45Release\src\System.IdentityModel.Tokens.Jwt\JwtSecurityTokenHandler.cs:line 658
   at Microsoft.Bot.Connector.JwtTokenExtractor.<ValidateTokenAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Bot.Connector.JwtTokenExtractor.<GetIdentityAsync>d__6.MoveNext()

Microsoft.Bot.Builder = 3.0.1 System.IdentityModel.Tokens.Jwt = 4.0.2.206221351

lamaq-git avatar Jul 12 '16 13:07 lamaq-git

@lamaq-git make sure that you set MicrosoftAppId and MicrosoftAppPassword to the value you got from dev portal. Also, if you run against emulator you can leave all the values blank to disable bot authentication.

msft-shahins avatar Jul 12 '16 17:07 msft-shahins

I had to revert the version on three of the libraries in my project in order to get this to work.

  • Microsoft.Rest.ClientRuntime - 1.8.2
  • Newtonsoft.Json - 8.0.3
  • System.IdentityModel.Tokens.Jwt - 4.0.2.206221351

BryanHall avatar Jul 12 '16 17:07 BryanHall

@Huskitch and @NanashiGIT how did you do the upgrade to new nuget package? It seems that you upgraded the System.IdentityModel.Tokens.Jwt to latest which is not the right dependency that builder expects.

To upgrade a v1 bot dependencies to use v3 builder and making sure that your dependencies are installed in the right way, follow below steps:

  • Go to nuget package manager for your project
  • Uninstall Microsoft.Bot.Builder v1
  • Uninstall Microsoft.Bot.Connector
  • Install Microsoft.Bot.Builder 3.0.1
  • update you web.config to have the new fields builder expect:
    <add key="BotId" value="YourBotId" />
    <add key="MicrosoftAppId" value="" />
    <add key="MicrosoftAppPassword" value="" />

Steps above make sure that you have the right dependencies. From what I see, it seems that you are upgrading all of the packages in the nuget package manager and end up with the latest JWT dependency.

msft-shahins avatar Jul 12 '16 17:07 msft-shahins

First i was on the correct 4.0.2 version. But then it began giving me the exception: ""iisexpress.exe Warning: 0 : Invalid token. System.IdentityModel.Tokens.SecurityTokenSignatureKeyNotFoundException: IDX10500: Signature validation failed. Unable to resolve SecurityKeyIdentifier: 'SecurityKeyIdentifier"". So i tried updating it to the newest version (Because it said, the Builder needs JWT >= 4.0.2). But that broke everything. And im 100% sure the MicrosoftAppId and MicrosoftAppPassword are correct. I've read somewhere, that the keys for the credentials might be a bit bugged (maybe emulator sends credentials not as MicrosoftAppId/MicrosoftAppPassword or something else). I dont know, but there is something broken.

NanashiGIT avatar Jul 12 '16 17:07 NanashiGIT

@NanashiGIT builder is dependent on System.IdentityModel.Tokens.Jwt 4.0.2 and when you upgrade to a new version of that package, .Net cannot load the right version of dll when loading dependencies. The error that you were getting when you had the right version of dependency was probably because of not having the right MicrosoftAppId and MicrosoftAppPassword set in your web.config or emulator.

I recommend setting those to empty, both in web.config and emulator (this disables auth), and make sure that your bot is working with auth turned off and after that set the values and check if auth is working correctly.

msft-shahins avatar Jul 12 '16 17:07 msft-shahins

@msft-shahins This is on a completely new project, from the V3 Bot Builder template.

Huskitch avatar Jul 12 '16 17:07 Huskitch

@msft-shahins I did disabled authentication for quite a bit and it worked. And as i said, the credentials are correct.

NanashiGIT avatar Jul 12 '16 17:07 NanashiGIT

@Huskitch then in V3 bot builder template, only update builder in the nuget package manager update tab. Do not update other nuget packages.

msft-shahins avatar Jul 12 '16 18:07 msft-shahins

@msft-shahins Whats with the BotId in the Webconfig? Unlike in the screenshots of the emulator, i cant specify an id. I also dont know where to find that id in the Bot Portal. May that be the culprit for the error messages?

NanashiGIT avatar Jul 12 '16 18:07 NanashiGIT

@NanashiGIT if auth doesn't work and you have the right dependencies, I am suspicious that you are using a wrong AppId and AppPassword. Go to dev portal and find the MicrosoftAppId for your bot, then go to Microsoft dev portal app list, find the app id and make sure that you have the right password for that app.

Bot Id == Bot Handle you see in the dev portal UI. builder is using it internally, if you configure it with the InMemoryBotDataStore (which is not the default data store). It is not used for the authentication and we will remove it soon and use MicrosoftAppId as the unique identifier for bot.

msft-shahins avatar Jul 12 '16 18:07 msft-shahins

@msft-shahins Thanks for your help. I realized that i have 2 AppSecrets set up for my app (and i dont mean primary/secondary). Maybe thats it and i have to remove one. I'll try that out tomorrow.

NanashiGIT avatar Jul 12 '16 18:07 NanashiGIT

Also make sure that in web.config is correct. They are not called AppId and AppSecret anymore:

    <add key="MicrosoftAppId" value="" />
    <add key="MicrosoftAppPassword" value="" />

msft-shahins avatar Jul 12 '16 18:07 msft-shahins