Пример #1
0
        private static async Task RunDemoAsync()
        {
            SalesOrder order1 = Program.GetSalesOrderSample("Account1", Guid.NewGuid().ToString());
            SalesOrder order2 = Program.GetSalesOrderSample("Account2", Guid.NewGuid().ToString());

            // Save the sales order into the container - all properties configured with Encryption Policy on the SalesOrder class
            // are encrypted using the encryption key per the policy configured for the path before sending to the Azure Cosmos DB service.
            await Program.containerWithEncryption.CreateItemAsync(
                order1,
                new PartitionKey(order1.AccountNumber));

            // Read the item back - decryption happens automatically based on the Encryption Policy configured for the Container.
            ItemResponse <SalesOrder> readResponse = await Program.containerWithEncryption.ReadItemAsync <SalesOrder>(order1.Id, new PartitionKey(order1.AccountNumber));

            SalesOrder readOrder = readResponse.Resource;

            Console.WriteLine("Creating Document 1: SubTotal : {0} After roundtripping post Decryption: {1}", order1.SubTotal, readOrder.SubTotal);

            order2.SubTotal = 552.4589m;
            await Program.containerWithEncryption.CreateItemAsync(
                order2,
                new PartitionKey(order2.AccountNumber));

            // Read the item back - decryption happens automatically based on the Encryption Policy configured for the Container.
            readResponse = await Program.containerWithEncryption.ReadItemAsync <SalesOrder>(order2.Id, new PartitionKey(order2.AccountNumber));

            readOrder = readResponse.Resource;

            Console.WriteLine("Creating Document 2: SubTotal : {0} After roundtripping post Decryption: {1}", order2.SubTotal, readOrder.SubTotal);

            // Query Demo.
            // Here SubTotal and OrderDate are encrypted properties.
            QueryDefinition withEncryptedParameter = containerWithEncryption.CreateQueryDefinition(
                "SELECT * FROM c where c.SubTotal = @SubTotal AND c.OrderDate = @OrderDate");

            await withEncryptedParameter.AddParameterAsync(
                "@SubTotal",
                order2.SubTotal,
                "/SubTotal");

            await withEncryptedParameter.AddParameterAsync(
                "@OrderDate",
                order2.OrderDate,
                "/OrderDate");

            FeedIterator <SalesOrder> queryResponseIterator;

            queryResponseIterator = containerWithEncryption.GetItemQueryIterator <SalesOrder>(withEncryptedParameter);

            FeedResponse <SalesOrder> readDocs = await queryResponseIterator.ReadNextAsync();

            Console.WriteLine("1) Query result: SELECT * FROM c where c.SubTotal = {0} AND c.OrderDate = {1}. Total Documents : {2} ", order2.SubTotal, order2.OrderDate, readDocs.Count);

            withEncryptedParameter = new QueryDefinition(
                "SELECT c.SubTotal FROM c");

            queryResponseIterator = containerWithEncryption.GetItemQueryIterator <SalesOrder>(withEncryptedParameter);

            readDocs = await queryResponseIterator.ReadNextAsync();

            Console.WriteLine("2) Query result: SELECT c.SubTotal FROM c. Total Documents : {0} ", readDocs.Count);
        }