Пример #1
0
        public async Task QueueRegionalSASTestAsync()
        {
#if NETCORE
            //CultureInfo currentCulture = CultureInfo.CurrentCulture;
            //CultureInfo.CurrentCulture = new CultureInfo("it");
#else
            string currentPrimaryLanguage = ApplicationLanguages.PrimaryLanguageOverride;
            ApplicationLanguages.PrimaryLanguageOverride = "it";
#endif

            CloudQueueClient client = GenerateCloudQueueClient();
            CloudQueue       queue  = client.GetQueueReference(GenerateNewQueueName());

            try
            {
                await queue.CreateAsync();

                string            messageContent = Guid.NewGuid().ToString();
                CloudQueueMessage message        = new CloudQueueMessage(messageContent);
                await queue.AddMessageAsync(message);

                // Prepare SAS authentication with full permissions
                string                       id          = Guid.NewGuid().ToString();
                DateTime                     start       = DateTime.UtcNow;
                DateTime                     expiry      = start.AddMinutes(30);
                QueuePermissions             permissions = new QueuePermissions();
                SharedAccessQueuePermissions queuePerm   = SharedAccessQueuePermissions.Add | SharedAccessQueuePermissions.ProcessMessages | SharedAccessQueuePermissions.Read | SharedAccessQueuePermissions.Update;
                permissions.SharedAccessPolicies.Add(id, new SharedAccessQueuePolicy()
                {
                    SharedAccessStartTime  = start,
                    SharedAccessExpiryTime = expiry,
                    Permissions            = queuePerm
                });

                await queue.SetPermissionsAsync(permissions);

                await Task.Delay(30 * 1000);

                string             sasTokenFromId   = queue.GetSharedAccessSignature(null, id);
                StorageCredentials sasCredsFromId   = new StorageCredentials(sasTokenFromId);
                CloudQueue         sasQueueFromId   = new CloudQueue(queue.Uri, sasCredsFromId);
                CloudQueueMessage  receivedMessage1 = await sasQueueFromId.PeekMessageAsync();

                Assert.AreEqual(messageContent, receivedMessage1.AsString);

                string             sasTokenFromPolicy = queue.GetSharedAccessSignature(permissions.SharedAccessPolicies[id], null);
                StorageCredentials sasCredsFromPolicy = new StorageCredentials(sasTokenFromPolicy);
                CloudQueue         sasQueueFromPolicy = new CloudQueue(queue.Uri, sasCredsFromPolicy);
                CloudQueueMessage  receivedMessage2   = await sasQueueFromPolicy.PeekMessageAsync();

                Assert.AreEqual(messageContent, receivedMessage2.AsString);
            }
            finally
            {
#if NETCORE
                //CultureInfo.CurrentCulture = currentCulture;
#else
                ApplicationLanguages.PrimaryLanguageOverride = currentPrimaryLanguage;
#endif
                queue.DeleteAsync().Wait();
            }
        }
        public void CloudQueueMessageEncryptionWithStrictMode()
        {
            // Create the Key to be used for wrapping.
            SymmetricKey aesKey = new SymmetricKey("symencryptionkey");

            // Create the resolver to be used for unwrapping.
            DictionaryKeyResolver resolver = new DictionaryKeyResolver();

            resolver.Add(aesKey);

            CloudQueueClient client = GenerateCloudQueueClient();
            string           name   = GenerateNewQueueName();
            CloudQueue       queue  = client.GetQueueReference(name);

            try
            {
                queue.CreateIfNotExists();

                string            messageStr = Guid.NewGuid().ToString();
                CloudQueueMessage message    = new CloudQueueMessage(messageStr);

                QueueEncryptionPolicy policy = new QueueEncryptionPolicy(aesKey, null);

                // Add message with policy.
                QueueRequestOptions createOptions = new QueueRequestOptions()
                {
                    EncryptionPolicy = policy
                };
                createOptions.RequireEncryption = true;

                queue.AddMessage(message, null, null, createOptions, null);

                // Set policy to null and add message while RequireEncryption flag is still set to true. This should throw.
                createOptions.EncryptionPolicy = null;

                TestHelper.ExpectedException <InvalidOperationException>(
                    () => queue.AddMessage(message, null, null, createOptions, null),
                    "Not specifying a policy when RequireEnryption is set to true should throw.");

                // Retrieve message
                QueueEncryptionPolicy retrPolicy      = new QueueEncryptionPolicy(null, resolver);
                QueueRequestOptions   retrieveOptions = new QueueRequestOptions()
                {
                    EncryptionPolicy = retrPolicy
                };
                retrieveOptions.RequireEncryption = true;

                CloudQueueMessage retrMessage = queue.GetMessage(null, retrieveOptions, null);

                // Update message with plain text.
                string updatedMessage = Guid.NewGuid().ToString("N");
                retrMessage.SetMessageContent(updatedMessage);

                queue.UpdateMessage(retrMessage, TimeSpan.FromSeconds(0), MessageUpdateFields.Content | MessageUpdateFields.Visibility);

                // Retrieve updated message with RequireEncryption flag but no metadata on the service. This should throw.
                TestHelper.ExpectedException <StorageException>(
                    () => queue.GetMessage(null, retrieveOptions, null),
                    "Retrieving with RequireEncryption set to true and no metadata on the service should fail.");

                // Set RequireEncryption to false and retrieve.
                retrieveOptions.RequireEncryption = false;
                queue.GetMessage(null, retrieveOptions, null);
            }
            finally
            {
                queue.DeleteIfExists();
            }
        }
        public static async IAsyncEnumerable <CloudQueueMessage> FetchAllMessagesAsync(this CloudQueue queue, TimeSpan visibilityTimeout)
        {
            var hasNext = true;

            while (hasNext)
            {
                hasNext = false;
                var messages = await queue.GetMessagesAsync(32, visibilityTimeout, null, null);

                foreach (var message in messages)
                {
                    hasNext = true;
                    yield return(message);
                }
            }
        }