如何从解密格式的azure数据表中获取加密的bot数据

时间:2018-03-26 02:53:00

标签: azure botframework

我使用Bot Application(visual C#)和bot framework创建了QNA bot .Bot状态数据存储在Azure数据表中。但是会话数据(用户类型数据)在Azure数据表中加密。我想要解密。我怎么解密呢?

1 个答案:

答案 0 :(得分:0)

  

会话数据(用户类型数据)在Azure数据表中加密。我想要解密。我怎么解密呢?

如果您想在存储后检索 ConversationData ,可以参考以下代码:

context.ConversationData.SetValue<string>("testval", "hello world");

string mes = "";

context.ConversationData.TryGetValue<string>("testval", out mes);

此外,对话历史记录与Azure表存储中的以下结构一起存储,如果您要检索对话历史记录并从Activity0提取数据,则可以尝试以下代码:

using Microsoft.Bot.Connector;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RetrieveHistory
{
    class Program
    {
        static void Main(string[] args)
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName={your_account_name};AccountKey={your_account_key};EndpointSuffix=core.windows.net");

            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

            CloudTable table = tableClient.GetTableReference("{your_botconversationhistory_table_name}");

            TableQuery<MessageEntity> query = new TableQuery<MessageEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "emulator|61b59bkmceee"));

            foreach (MessageEntity entity in table.ExecuteQuery(query))
            {
                string myactivity = "";
                using (var msi = new MemoryStream(entity.Activity0))
                using (var mso = new MemoryStream())
                {
                    using (var gs = new GZipStream(msi, CompressionMode.Decompress))
                    {
                        gs.CopyTo(mso);
                    }
                    myactivity = Encoding.UTF8.GetString(mso.ToArray());
                }

                Activity activity = JsonConvert.DeserializeObject<Activity>(myactivity);

                Console.WriteLine("{0}, {1}\t{2}\t{3}\t{4}", entity.PartitionKey, entity.RowKey,
                    entity.From, entity.Recipient, activity.Text);
            }



            Console.ReadLine();
        }



        public class MessageEntity : TableEntity
        {
            public MessageEntity(string pk, string rk)
            {
                this.PartitionKey = pk;
                this.RowKey = rk;
            }

            public MessageEntity() { }

            public string From { get; set; }

            public string Recipient { get; set; }

            public byte[] Activity0 { get; set; }
        }
    }
}

属性和类型:

enter image description here

示例实体:

enter image description here

注意:您可以创建一个控制台应用程序并安装以下软件包,并使用我提供的代码进行测试以检索历史记录。

packages.config:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net45" />
  <package id="Microsoft.Bot.Connector" version="3.13.1" targetFramework="net46" />
  <package id="Microsoft.IdentityModel.Logging" version="1.1.4" targetFramework="net46" />
  <package id="Microsoft.IdentityModel.Protocols" version="2.1.4" targetFramework="net46" />
  <package id="Microsoft.IdentityModel.Protocols.OpenIdConnect" version="2.1.4" targetFramework="net46" />
  <package id="Microsoft.IdentityModel.Tokens" version="5.1.4" targetFramework="net46" />
  <package id="Microsoft.Rest.ClientRuntime" version="2.3.2" targetFramework="net46" />
  <package id="Newtonsoft.Json" version="10.0.2" targetFramework="net45" />
  <package id="System.IdentityModel.Tokens.Jwt" version="5.1.4" targetFramework="net46" />
  <package id="WindowsAzure.Storage" version="9.1.0" targetFramework="net45" />
</packages>