.net - 异常:使用 context.Forward() 和 AuthBot 时堆栈为空?

标签 .net azure authentication exception botframework

我正在遵循 AuthBot 教程中的 SampleAADv2Bot 示例。我想使用已配置的 Azure Active Directory 对我的机器人进行身份验证。我按照下图设置了 Web.Config。我的对话框类中有与 here 列出的相同的代码除了我的类名称是 LoginDialog.cs。但是我在模拟器中看到了这个异常:

enter image description here

当我在代码中(在 LoginDialog.cs 中)进行此调用时发生错误:

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

这是我的 LoginDialog 类。 ResumeAfterAuth() 已实现。

登录对话框.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)
    { 
        //...
    }

}

编辑#1:在我的代码中设置断点后,看起来错误发生在此处的 WebApiConfig.cs 文件中:

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

编辑#2:到达图中所示的行后发生错误:

enter image description here

到达此行时,消息为空。它立即离开该函数并返回到该行:

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

然后返回到编辑 #1 中所示的 WebApiConfig 中的 JSON 行,运行函数 JsonSerializerSettings() 两次,然后引发异常。

这是异常的输出 View :

enter image description here

最佳答案

我发现了这个问题。我需要在 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"];

关于.net - 异常:使用 context.Forward() 和 AuthBot 时堆栈为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45168712/

相关文章:

events - Symfony 2 中的 kernel.request 事件中的身份验证 token 始终为 null?

c# - 在代码中获取硬盘上最大的目录大小

c# - 如何使用正则表达式检查特殊字符

c# - 检查对象是否是 T 的子类的扩展方法

angular - 如何在 Angular 中访问 Azure 静态 Web 应用程序应用程序设置?

azure - 应用程序洞察 -> 导出 -> Power BI 数据仓库架构

.net - 如果没有正确的.NET Framework版本,为什么不引发异常?

azure - 使用 npm 包安装 Azure Functions 时出错

java - 将多个密码存储到数据库中的最佳方法

google-app-engine - 作为 OpenId 提供者的 Appengine 应用程序。是否可以?