From f5e84eba05ce28ddc51d839ba84ae8722da418b6 Mon Sep 17 00:00:00 2001 From: Tom Laird-McConnell Date: Sun, 6 May 2018 15:31:16 -0700 Subject: [PATCH] fix OAuth with emulator and no credentials (#4570) --- CSharp/EchoBot/Web.config | 9 +++-- .../ConnectorEx/IConnectorClientFactory.cs | 38 ++++++++++--------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/CSharp/EchoBot/Web.config b/CSharp/EchoBot/Web.config index f263970d84..0e6b423cfc 100644 --- a/CSharp/EchoBot/Web.config +++ b/CSharp/EchoBot/Web.config @@ -5,9 +5,12 @@ --> - - - + + + + diff --git a/CSharp/Library/Microsoft.Bot.Builder/ConnectorEx/IConnectorClientFactory.cs b/CSharp/Library/Microsoft.Bot.Builder/ConnectorEx/IConnectorClientFactory.cs index 7fe85bb670..4b74d3e8a1 100644 --- a/CSharp/Library/Microsoft.Bot.Builder/ConnectorEx/IConnectorClientFactory.cs +++ b/CSharp/Library/Microsoft.Bot.Builder/ConnectorEx/IConnectorClientFactory.cs @@ -114,30 +114,34 @@ public ConnectorClientFactory(IAddress address, MicrosoftAppCredentials credenti if (!oauthClients.TryGetValue(key, out oauthClient)) { - if (IsEmulator(this.address) && emulateOAuthCards.Value) + // only create the oauthclient if we have credentials + if (!String.IsNullOrEmpty(credentials?.MicrosoftAppId) && !String.IsNullOrEmpty(credentials?.MicrosoftAppPassword)) { - // for emulator using emulated OAuthCards we should use serviceUri of the emulator - oauthClient = new OAuthClient(this.serviceUri, this.credentials); - } - else - { - if (!string.IsNullOrEmpty(settingsOAuthApiUrl.Value)) + if (IsEmulator(this.address) && emulateOAuthCards.Value) { - oauthClient = new OAuthClient(new Uri(settingsOAuthApiUrl.Value), this.credentials); + // for emulator using emulated OAuthCards we should use serviceUri of the emulator + oauthClient = new OAuthClient(this.serviceUri, this.credentials); } else { - oauthClient = new OAuthClient(this.credentials); + if (!string.IsNullOrEmpty(settingsOAuthApiUrl.Value)) + { + oauthClient = new OAuthClient(new Uri(settingsOAuthApiUrl.Value), this.credentials); + } + else + { + oauthClient = new OAuthClient(this.credentials); + } } - } - if (IsEmulator(this.address)) - { - // Send the mode notification (emulated OAuthCards or not) to the emulator - Task.Run(async () => await oauthClient.OAuthApi.SendEmulateOAuthCardsAsync(emulateOAuthCards.Value).ConfigureAwait(false)).Wait(); - } + if (IsEmulator(this.address)) + { + // Send the mode notification (emulated OAuthCards or not) to the emulator + Task.Run(async () => await oauthClient.OAuthApi.SendEmulateOAuthCardsAsync(emulateOAuthCards.Value).ConfigureAwait(false)).Wait(); + } - oauthClients[key] = oauthClient; + oauthClients[key] = oauthClient; + } } } @@ -207,7 +211,7 @@ private static bool GetEmulateOAuthCardsSetting(string key = "EmulateOAuthCards" // default back to false result = false; } - + return result; } }