2017-07-18 2 views
1

AuthBotチュートリアルのSampleAADv2Botの例に従っています。すでに設定されているAzure Active Directoryを使用してボットを認証したいと思います。 Web.Configを以下のように設定しました。私はクラス名がLoginDialog.csであることを除いて、hereと同じコードをダイアログクラスに持っています。しかし私は、エミュレータでこの例外を参照してください。例外:context.Forward()およびAuthBotを使用している場合、スタックは空です。

enter image description here

私は私のコードでは、この呼び出しを行うときにエラーが(LoginDialog.csに)起こる:ここ

await context.Forward(new AzureAuthDialog(AuthSettings.Scopes), this.ResumeAfterAuth, message, CancellationToken.None); 

は私LoginDialogクラスです。 ResumeAfterAuth()が実装されています。

LoginDialog.cs:ここ

// Copyright (c) Microsoft. All rights reserved. Licensed under the MIT 
license. See full license at the bottom of this file. 
namespace Bot.Dialogs 
{ 
    using System; 
    using System.Threading; 
    using System.Threading.Tasks; 
    using AuthBot; 
    using AuthBot.Dialogs; 
    using AuthBot.Models; 
    using Microsoft.Bot.Builder.Dialogs; 
    using Microsoft.Bot.Connector; 

    [Serializable] 
    public class LoginDialog : IDialog<string> 
    { 
     public async Task StartAsync(IDialogContext context) 
     { 
      context.Wait(MessageReceivedAsync); 
     } 

     public async Task TokenSample(IDialogContext context) 
     { 
      //endpoint v2 
      var accessToken = await context.GetAccessToken(AuthSettings.Scopes); 

      if (string.IsNullOrEmpty(accessToken)) 
      { 
       return; 
      } 

      await context.PostAsync($"Your access token is: {accessToken}"); 

      context.Wait(MessageReceivedAsync); 
     } 

     public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> item) 
     { 
      var message = await item; 

      if (message.Text == "logon") 
      { 
       //endpoint v2 
       if (string.IsNullOrEmpty(await context.GetAccessToken(AuthSettings.Scopes))) 
       { 
        await context.Forward(new AzureAuthDialog(AuthSettings.Scopes), this.ResumeAfterAuth, message, CancellationToken.None); 
       } 
       else 
       { 
        context.Wait(MessageReceivedAsync); 
       } 
      } 
      else if (message.Text == "echo") 
      { 
       await context.PostAsync("echo"); 

       context.Wait(this.MessageReceivedAsync); 
      } 
      else if (message.Text == "token") 
      { 
       await TokenSample(context); 
      } 
      else if (message.Text == "logout") 
      { 
       await context.Logout(); 
       context.Wait(this.MessageReceivedAsync); 
      } 
      else 
      { 
       context.Wait(MessageReceivedAsync); 
      } 
     } 

     private async Task ResumeAfterAuth(IDialogContext context, IAwaitable<string> result) 
     { 
      var message = await result; 

      await context.PostAsync(message); 
      context.Wait(MessageReceivedAsync); 
     } 
    } 
} 

は私MessagesController.csです:

namespace Bot 
{ 
    //[BotAuthentication] 
    public class MessagesController : ApiController 
    { 

     public async Task<HttpResponseMessage> Post([FromBody]Activity activity) 
     { 

      if (activity.Type == ActivityTypes.ConversationUpdate) 
      { 
      } 
      else if (activity.Type == ActivityTypes.Message) 
      { 

       if (activity.Text.Contains("logon") || activity.Text.Contains("login")) 
       { 
        await Conversation.SendAsync(activity,() => new Dialogs.LoginDialog()); 
       } 
       else 
       { 
        // Sends user's Id and Name to RootDialog Class 
        StateClient stateClient = activity.GetStateClient(); 
        BotData userData = await stateClient.BotState.GetUserDataAsync(activity.ChannelId, activity.From.Id); 
        userData.SetProperty<string>("UserId", activity.From.Id); 
        userData.SetProperty<string>("Name", activity.From.Name); 

        // send these values in the Context 
        await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData); 


        await Conversation.SendAsync(activity,() => new Dialogs.RootDialog()); 
       } 

      } 
      else if (activity.Type == ActivityTypes.Invoke) 
      { 
       //... 

      } 
      else 
      { 
       HandleSystemMessage(activity); 
      } 
      var response = Request.CreateResponse(HttpStatusCode.OK); 
      return response; 
     } 


    private Activity HandleSystemMessage(Activity activity) 
    { 
     //... 
    } 

} 

EDIT#1:エラーがWebApiConfigで起こっているように私のコードにブレークポイントを設定した後に見えます。ここにcsファイル:

JsonConvert.DefaultSettings =() => new JsonSerializerSettings() 
{ 
    ContractResolver = new CamelCasePropertyNamesContractResolver(), 
       Formatting = Newtonsoft.Json.Formatting.Indented, 
       NullValueHandling = NullValueHandling.Ignore, 
}; 

EDIT#2:tに表示されている行に達した後にエラーが発生します。彼の絵:

enter image description here

この行がメッセージに達しているnullです。それはすぐにこの機能を出て、ラインに戻っ:

await context.Forward(new AzureAuthDialog(AuthSettings.Scopes), this.ResumeAfterAuth, message, CancellationToken.None);

をして、EDIT#1に示すWebApiConfigでJSONラインに戻り、二度機能JsonSerializerSettings()を実行して、例外がスローされます。ここで

は、例外の出力ビューです:

enter image description here

+0

あなたは 'ResumeAfterAuth'メソッドを実装していますか?コードを追加できますか? –

+0

私のコードを追加しました。私は2つのダイアログを使用しているので、ResumeAfterAuthで何か違うことをする必要がありますか? – Megan

+0

また、何らかの理由でResumeAfterAuth()の 'var message = await result;'行で例外が発生していると思いますか? – Megan

答えて

1

私は問題を発見しました。私は私のGlobal.asax.csファイルの行GlobalConfiguration.Configure(WebApiConfig.Register);の下で、これらの行を追加する必要:

AuthSettings.Mode = ConfigurationManager.AppSettings["ActiveDirectory.Mode"]; 
AuthSettings.EndpointUrl = ConfigurationManager.AppSettings["ActiveDirectory.EndpointUrl"]; 
AuthSettings.Tenant = ConfigurationManager.AppSettings["ActiveDirectory.Tenant"]; 
AuthSettings.RedirectUrl = ConfigurationManager.AppSettings["ActiveDirectory.RedirectUrl"]; 
AuthSettings.ClientId = ConfigurationManager.AppSettings["ActiveDirectory.ClientId"]; 
AuthSettings.ClientSecret = ConfigurationManager.AppSettings["ActiveDirectory.ClientSecret"]; 
関連する問題