我正在关注AuthBot教程中的SampleAADv2Bot示例。我想使用已配置的Azure Active Directory对我的bot进行身份验证。我用下面的图片设置了我的Web.Config。我的对话框类中的代码与列出的here相同,只是我的类名是LoginDialog.cs。我在模拟器中看到了这个例外:
当我在我的代码中进行此调用时(在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)
{
//...
}
}
编辑#1:在我的代码中设置断点后,看起来错误发生在WebApiConfig.cs文件中:
JsonConvert.DefaultSettings = () => new JsonSerializerSettings()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Formatting = Newtonsoft.Json.Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore,
};
编辑#2:在到达此图片中显示的行后发生错误:
当达到此行时,消息为空。它会立即离开此功能并返回到该行:
await context.Forward(new AzureAuthDialog(AuthSettings.Scopes), this.ResumeAfterAuth, message, CancellationToken.None);
然后返回编辑#1中显示的WebApiConfig中的JSON行,运行函数JsonSerializerSettings()
两次,然后抛出异常。
以下是例外的输出视图:
答案 0 :(得分: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"];