C# 会话日志与AAD v1身份验证相结合

C# 会话日志与AAD v1身份验证相结合,c#,botframework,C#,Botframework,我在将AAD1身份验证和自定义对话记录器添加到聊天机器人时遇到问题。一个或另一个工作正常,但当两者结合时,我会得到HTTP超时。如蒙协助,将不胜感激。相关代码如下: Global.asax.cs protected void Application_Start() { // Adding DocumentDB endpoint and primary key var docDbServiceEndpoint = new Uri("-------------

我在将AAD1身份验证和自定义对话记录器添加到聊天机器人时遇到问题。一个或另一个工作正常,但当两者结合时,我会得到HTTP超时。如蒙协助,将不胜感激。相关代码如下:

Global.asax.cs

protected void Application_Start()
    {
        // Adding DocumentDB endpoint and primary key
        var docDbServiceEndpoint = new Uri("-----------------------------------");//REMOVED Uri for question, no issue with connection as is
        var docDbKey = "--------------------------------------------"; //REMOVED Key for question, no issue with connection as is

        Conversation.UpdateContainer(builder =>
        {
            builder.RegisterModule(new AzureModule(Assembly.GetExecutingAssembly()));

            var store = new DocumentDbBotDataStore(docDbServiceEndpoint, docDbKey); // requires Microsoft.BotBuilder.Azure Nuget package                 

            builder.RegisterType<DebugActivityLogger>().AsImplementedInterfaces().InstancePerDependency();
        });
        //authorization stuff
        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"];

        GlobalConfiguration.Configure(WebApiConfig.Register);
    }

这项计划已经停止。如注释中所述,您应该使用用于AADv1身份验证的。BotAuth支持状态数据对话支持(这样您就不会收到不推荐使用的状态客户端警告)。

两个问题-1)您使用哪个软件包进行
AAD v1
身份验证?2) 为什么不使用状态数据API将对话记录到DocumentDB中?1)为了进行测试,我正在使用中断的AuthBot Nuget软件包,并使用graph.microsoft.com进行登录,以生成聊天代码2)我当前正在使用DocumentDB,但我需要保存对话文本,而不仅仅是状态数据。我是否错过了状态数据API中的自定义实现?Authbot已被弃用。我建议您使用BotAuth-。状态数据API包括对话、私人对话和机器人数据。我不知道它有我需要的信息。谢谢你的帮助!好的。你应该得到荣誉
[Serializable]
public class AuthenticationHelper : IDialog<string>
{
    public async Task StartAsync(IDialogContext context)
    {
        context.Wait(ProcessMessageAsync);
    }

    public async Task ProcessMessageAsync(IDialogContext context, IAwaitable<IMessageActivity> item)
    {
        var message = await item;
        if (string.IsNullOrEmpty(await context.GetAccessToken("https://graph.microsoft.com/")))
        {
            //NO ACCESS TOKEN, GET IT
            await context.Forward(new AzureAuthDialog("https://graph.microsoft.com"), this.ProcessAuthResultAsync, message, System.Threading.CancellationToken.None);                
        }
        else
        {
            //have token                     
            await context.Forward(new LuisAskQuestionDialog(), this.QuitMessageReceivedAsync, message, System.Threading.CancellationToken.None);
        }
    }

    public async Task ProcessAuthResultAsync(IDialogContext context, IAwaitable<string> result)
    {            
        var message = await result;
        await context.PostAsync(message);

        context.Wait(ProcessMessageAsync);
    }

    protected async Task QuitMessageReceivedAsync(IDialogContext context, IAwaitable<object> item)
    {            
        var message = await item;
        //StartRecordingProcess();
        context.Done(message);
    }        
}
public class DebugActivityLogger : IActivityLogger
{
    private const string EndpointUrl = "------------------------------";
    private const string PrimaryKey = "------------------------------------";
    private DocumentClient client;

    // ADD THIS PART TO YOUR CODE        

    public async Task LogAsync(IActivity activity)
    {
        //Update this information
        //What this needs to have: ConversationID, From, To, Date, Message
        //Get all the texts information ready for upload;
        //Get connection to table
        //upload the inforamtion onto the table
        //disconnect from the table
        // Retrieve the storage account from the connection string.

        //This Task is called to intercept messages
        var fromid = activity.From.Id;
        var toId = activity.Recipient.Id;
        var chatMessage = activity.AsMessageActivity()?.Text;            
        var timeStamp = activity.Timestamp;
        var conversationId = activity.Conversation.Id;
        //timestamp converted to string.
        string strTimeStamp = timeStamp.ToString();

        try
        {
            this.client = new DocumentClient(new Uri(EndpointUrl), PrimaryKey);
            await this.client.CreateDatabaseIfNotExistsAsync(new Database { Id = "botdb" });
            await this.client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri("botdb"), new DocumentCollection { Id = "botcollection" });

            ChatLogEntity chatLog1 = new ChatLogEntity
            {
                TimeStamp = strTimeStamp,
                ConversationId = conversationId,
                FromID = fromid,
                ToID = toId,
                ChatMessage = chatMessage                    
            };

            await this.CreateChatDocumentIfNotExists("botdb", "botcollection", chatLog1);
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);                
        }            

    }        
//entity class for demo purposes
    // ADD THIS PART TO YOUR CODE
    private async Task CreateChatDocumentIfNotExists(string databaseName, string collectionName, ChatLogEntity chatEntity)
    {
        try
        {
            await this.client.ReadDocumentAsync(UriFactory.CreateDocumentUri(databaseName, collectionName, chatEntity.TimeStamp));

        }
        catch (DocumentClientException de)
        {
            if (de.StatusCode == HttpStatusCode.NotFound)
            {
                await this.client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(databaseName, collectionName), chatEntity);                    
            }
            else
            {
                throw;
            }
        }
    }
    public class ChatLogEntity
    {            
        [JsonProperty(PropertyName = "timestamp")]
        public string TimeStamp { get; set; }
        public string ConversationId { get; set; }

        public string ToID { get; set; }
        public string FromID { get; set; }
        public string ChatMessage { get; set; }
        public override string ToString()
        {
            return JsonConvert.SerializeObject(this);
        }
    }        
}