gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:05:07 28 4
gpt4 key购买 nike

我正在遵循 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com