public async Task QueuePermissionsRawPermissions(string permissionsString) { // Arrange await using DisposingQueue test = await GetTestQueueAsync(); QueueSasBuilder queueSasBuilder = new QueueSasBuilder { StartsOn = Recording.UtcNow.AddHours(-1), ExpiresOn = Recording.UtcNow.AddHours(1), QueueName = test.Queue.Name }; queueSasBuilder.SetPermissions( rawPermissions: permissionsString, normalize: true); StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(TestConfigDefault.AccountName, TestConfigDefault.AccountKey); QueueUriBuilder queueUriBuilder = new QueueUriBuilder(test.Queue.Uri) { Sas = queueSasBuilder.ToSasQueryParameters(sharedKeyCredential) }; QueueClient sasQueueClient = new QueueClient(queueUriBuilder.ToUri(), GetOptions()); // Act await sasQueueClient.PeekMessagesAsync(); }
public async Task <Uri> GetQueueSas(string name, StorageType storageType, QueueSasPermissions permissions, TimeSpan?duration) { var queue = await GetQueueClient(name, storageType) ?? throw new Exception($"unable to queue object, no such queue: {name}"); var sasaBuilder = new QueueSasBuilder(permissions, DateTimeOffset.UtcNow + (duration ?? DEFAULT_DURATION)); return(queue.GenerateSasUri(sasaBuilder)); }
public void QueueSasBuilder_NullSharedKeyCredentialTest() { // Arrange var constants = TestConstants.Create(this); var queueName = GetNewQueueName(); QueueSasBuilder queueSasBuilder = BuildQueueSasBuilder(constants, queueName, includeVersion: true); // Act Assert.Throws <ArgumentNullException>(() => queueSasBuilder.ToSasQueryParameters(null), "sharedKeyCredential"); }
public SasQueryParameters GetNewQueueServiceSasCredentials(string queueName, StorageSharedKeyCredential sharedKeyCredentials = default) { var builder = new QueueSasBuilder { QueueName = queueName, Protocol = SasProtocol.None, StartsOn = Recording.UtcNow.AddHours(-1), ExpiresOn = Recording.UtcNow.AddHours(+1), IPRange = new SasIPRange(IPAddress.None, IPAddress.None) }; builder.SetPermissions(QueueAccountSasPermissions.Read | QueueAccountSasPermissions.Update | QueueAccountSasPermissions.Process | QueueAccountSasPermissions.Add); return(builder.ToSasQueryParameters(sharedKeyCredentials ?? GetNewSharedKeyCredentials())); }
private QueueSasBuilder BuildQueueSasBuilder(TestConstants constants, string queueName) { var queueSasBuilder = new QueueSasBuilder { Version = null, Protocol = constants.Sas.Protocol, StartsOn = constants.Sas.StartTime, ExpiresOn = constants.Sas.ExpiryTime, IPRange = constants.Sas.IPRange, Identifier = constants.Sas.Identifier, QueueName = queueName, }; queueSasBuilder.SetPermissions(Permissions); return(queueSasBuilder); }
public async Task QueuePermissionsRawPermissions_Invalid() { // Arrange await using DisposingQueue test = await GetTestQueueAsync(); QueueSasBuilder queueSasBuilder = new QueueSasBuilder { StartsOn = Recording.UtcNow.AddHours(-1), ExpiresOn = Recording.UtcNow.AddHours(1), QueueName = test.Queue.Name }; // Act TestHelper.AssertExpectedException( () => queueSasBuilder.SetPermissions( rawPermissions: "ptsdfsd", normalize: true), new ArgumentException("s is not a valid SAS permission")); }
private QueueSasBuilder BuildQueueSasBuilder(TestConstants constants, string queueName, bool includeVersion) { var queueSasBuilder = new QueueSasBuilder { Version = null, Protocol = constants.Sas.Protocol, StartTime = constants.Sas.StartTime, ExpiryTime = constants.Sas.ExpiryTime, Permissions = Permissions, IPRange = constants.Sas.IPRange, Identifier = constants.Sas.Identifier, QueueName = queueName, }; if (includeVersion) { queueSasBuilder.Version = constants.Sas.Version; } return(queueSasBuilder); }
public void ToSasQueryParameters_IdentifierTest() { // Arrange var constants = TestConstants.Create(this); var queueName = GetNewQueueName(); QueueSasBuilder sasBuilder = new QueueSasBuilder { Identifier = constants.Sas.Identifier, QueueName = queueName, Protocol = SasProtocol.Https, }; // Act SasQueryParameters sasQueryParameters = sasBuilder.ToSasQueryParameters(constants.Sas.SharedKeyCredential); // Assert Assert.AreEqual(constants.Sas.Identifier, sasQueryParameters.Identifier); Assert.AreEqual(SasProtocol.Https, sasQueryParameters.Protocol); Assert.AreEqual(SasQueryParametersInternals.DefaultSasVersionInternal, sasQueryParameters.Version); }
public void QueueSasBuilder_ToSasQueryParameters_NoVersionTest() { // Arrange var constants = TestConstants.Create(this); var queueName = GetNewQueueName(); QueueSasBuilder queueSasBuilder = BuildQueueSasBuilder(constants, queueName, includeVersion: false); var signature = BuildSignature(constants, queueName, includeVersion: false); // Act var sasQueryParameters = queueSasBuilder.ToSasQueryParameters(constants.Sas.SharedKeyCredential); // Assert Assert.AreEqual(SasQueryParameters.DefaultSasVersion, sasQueryParameters.Version); Assert.IsNull(sasQueryParameters.Services); Assert.IsNull(sasQueryParameters.ResourceTypes); Assert.AreEqual(SasProtocol.Https, sasQueryParameters.Protocol); Assert.AreEqual(constants.Sas.StartTime, sasQueryParameters.StartsOn); Assert.AreEqual(constants.Sas.ExpiryTime, sasQueryParameters.ExpiresOn); Assert.AreEqual(constants.Sas.IPRange, sasQueryParameters.IPRange); Assert.AreEqual(constants.Sas.Identifier, sasQueryParameters.Identifier); Assert.AreEqual(string.Empty, sasQueryParameters.Resource); Assert.AreEqual(Permissions, sasQueryParameters.Permissions); Assert.AreEqual(signature, sasQueryParameters.Signature); }
/// <summary> /// Queue SAS Builder /// https://docs.microsoft.com/en-us/dotnet/api/azure.storage.sas.queuesasbuilder?view=azure-dotnet-preview /// /// </summary> /// <returns></returns> public static string QueueSasBuilder(string accountName, string accountKey, string queueName, string PolicyName = "") { // TODO - ExpiryTime must be omitted if Identifier is specified. // Create a SAS token that's valid a short interval. QueueSasBuilder queueSasBuilder; try{ queueSasBuilder = new QueueSasBuilder() { QueueName = queueName, // string - The name of the queue // Identifier = // string - access policy specified for the container // IPRange = //Permissions = "adlpruw", // string - Add, Delete, List, Process, Read, Update, Write (Queue Account SAS) //Permissions = "apru", // string - Add, Process, Read, Update (Queue SAS) Permissions = SAS_Utils.SAS.sp.v, // Protocol = // SasProtocol - Https, HttpsAndHttp, None, Value // StartTime = - DateTimeOffset.UtcNow // OK // QueueName = ExpiryTime = SAS_Utils.SAS.seDateTime, // DateTimeOffset - DateTimeOffset.UtcNow.AddMinutes(60) // OK Version = SAS_Utils.SAS.sv.v // String - sv }; // Adding the optional fields commented out above //----------------------------------------------------------- // ExpiryTime must be omitted if it has been specified in an associated stored access policy. if (PolicyName != "") { queueSasBuilder.Identifier = PolicyName; } if (!String.IsNullOrEmpty(SAS_Utils.SAS.st.v)) { queueSasBuilder.StartTime = SAS_Utils.SAS.stDateTime; } if (!String.IsNullOrEmpty(SAS_Utils.SAS.spr.v)) { queueSasBuilder.Protocol = Get_SasProtocol(SAS_Utils.SAS.spr.v); // SasProtocol - Https, HttpsAndHttp, None } if (!String.IsNullOrEmpty(SAS_Utils.SAS.sip.v)) { queueSasBuilder.IPRange = new IPRange(new System.Net.IPAddress(SAS_Utils.fromIP), (SAS_Utils.toIP[0] == 0 ? null : new System.Net.IPAddress(SAS_Utils.toIP))); // IPRange - StartIP, optional EndIP } if (!String.IsNullOrEmpty(SAS_Utils.SAS.queueName.v)) { queueSasBuilder.QueueName = SAS_Utils.SAS.queueName.v; } } catch (Exception ex) { MessageBox.Show("Error on creating Queue Service SAS\n\n" + ex.ToString(), "Invalid SAS parameters", MessageBoxButton.OK, MessageBoxImage.Exclamation); return(""); } //----------------------------------------------------------- StorageSharedKeyCredential Credential = new StorageSharedKeyCredential(accountName, accountKey); // Use the key to get the SAS token. string sasToken = queueSasBuilder.ToSasQueryParameters(Credential).ToString(); return(sasToken); }
static async Task Main(string[] args) { var o = Microsoft.Extensions.Options.Options.Create(new QueueSenderOptions { TenantId = "020b0cf3-d6b2-464e-9b2d-45e124244428", ClientId = "1d6309c9-3e22-42d8-bb59-3341a6f8632b", ClientSecret = "bvrCA5AXaNI_9v3j2rMQ8jrL4f_Zaf.t.0", QueueUri = new Uri("https://stefsa.queue.core.windows.net/example-qa") });; var client = new QueueSender(o); var messageId = await client.SendAsync(new { }); Console.WriteLine(messageId); return; Console.WriteLine("credentialQueueApi"); var credentialQueueApi = new ClientSecretCredential("020b0cf3-d6b2-464e-9b2d-45e124244428", "1d6309c9-3e22-42d8-bb59-3341a6f8632b", "bvrCA5AXaNI_9v3j2rMQ8jrL4f_Zaf.t.0"); var client1 = new QueueClient(new Uri("https://stefsa.queue.core.windows.net/example-q"), credentialQueueApi); client1.SendMessage("credentialQueueApi" + DateTime.UtcNow); var s = new StorageSharedKeyCredential("stefsa", "K4HEyKrlX3Bv5kI7Xua73+7hgMdU+X1dFXeNzxEZkN1Y6cAGQsWaeAdJMZhAzYTRXT2eR79TUlhb17x9QpbkXg=="); var client3 = new QueueClient(new Uri("https://stefsa.queue.core.windows.net/example-q"), s); client3.SendMessage("StorageSharedKeyCredential" + DateTime.UtcNow); // Create a SAS token that's valid for one hour. var sasBuilder = new QueueSasBuilder { QueueName = "example-q", StartsOn = DateTimeOffset.UtcNow, ExpiresOn = DateTimeOffset.UtcNow.AddHours(1) }; // Specify read permissions for the SAS. sasBuilder.SetPermissions(QueueSasPermissions.Add); // Use the key to get the SAS token. string sasToken = sasBuilder.ToSasQueryParameters(s).ToString(); // Construct the full URI, including the SAS token. UriBuilder fullUri = new UriBuilder { Scheme = "https", Host = "stefsa.queue.core.windows.net", Path = "example-q", Query = sasToken }; var q4 = new QueueClient(fullUri.Uri, null); q4.SendMessage("QueueSasBuilder " + DateTime.UtcNow); Console.WriteLine("tc"); var tc = new ClientSecretCredential("020b0cf3-d6b2-464e-9b2d-45e124244428", "4bd5d21b-1a20-4e86-a06a-b7f8598030b3", "jf864wuqkGDGQHf5uYt--be~5nlc3r-nX8"); var client2 = new QueueClient(new Uri("https://stefsa.queue.core.windows.net/example-q"), tc); client2.SendMessage("tc" + DateTime.UtcNow); }