public void FileSasBuilder_ToSasQueryParameters_NoVersionTest() { // Arrange var constants = TestConstants.Create(this); var shareName = GetNewShareName(); var filePath = GetNewDirectoryName(); ShareSasBuilder fileSasBuilder = BuildFileSasBuilder(includeVersion: false, includeFilePath: false, constants, shareName, filePath); var signature = BuildSignature(includeFilePath: false, includeVersion: false, constants, shareName, filePath); // Act var sasQueryParameters = fileSasBuilder.ToSasQueryParameters(constants.Sas.SharedKeyCredential); // Assert Assert.AreEqual(SasQueryParameters.DefaultSasVersion, sasQueryParameters.Version); Assert.IsNull(sasQueryParameters.Services); Assert.IsNull(sasQueryParameters.ResourceTypes); Assert.AreEqual(constants.Sas.Protocol, 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(Constants.Sas.Resource.Share, sasQueryParameters.Resource); Assert.AreEqual(Permissions, sasQueryParameters.Permissions); Assert.AreEqual(signature, sasQueryParameters.Signature); AssertResponseHeaders(constants, sasQueryParameters); }
private ShareSasBuilder BuildFileSasBuilder(bool includeVersion, bool includeFilePath, TestConstants constants, string shareName, string filePath) { var fileSasBuilder = new ShareSasBuilder { Version = null, Protocol = constants.Sas.Protocol, StartsOn = constants.Sas.StartTime, ExpiresOn = constants.Sas.ExpiryTime, IPRange = constants.Sas.IPRange, Identifier = constants.Sas.Identifier, ShareName = shareName, FilePath = "", CacheControl = constants.Sas.CacheControl, ContentDisposition = constants.Sas.ContentDisposition, ContentEncoding = constants.Sas.ContentEncoding, ContentLanguage = constants.Sas.ContentLanguage, ContentType = constants.Sas.ContentType }; fileSasBuilder.SetPermissions(ShareFileSasPermissions.All); if (includeVersion) { fileSasBuilder.Version = constants.Sas.Version; } if (includeFilePath) { fileSasBuilder.FilePath = filePath; } return(fileSasBuilder); }
public async Task SharePermissionsRawPermissions(string permissionsString) { // Arrange await using DisposingShare test = await GetTestShareAsync(); ShareSasBuilder blobSasBuilder = new ShareSasBuilder { StartsOn = Recording.UtcNow.AddHours(-1), ExpiresOn = Recording.UtcNow.AddHours(1), ShareName = test.Share.Name }; blobSasBuilder.SetPermissions( rawPermissions: permissionsString, normalize: true); StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(TestConfigDefault.AccountName, TestConfigDefault.AccountKey); ShareUriBuilder blobUriBuilder = new ShareUriBuilder(test.Share.Uri) { Sas = blobSasBuilder.ToSasQueryParameters(sharedKeyCredential) }; ShareClient sasShareClient = new ShareClient(blobUriBuilder.ToUri(), GetOptions()); // Act await sasShareClient.GetRootDirectoryClient().GetPropertiesAsync(); }
// </snippet_CreateShare> // <snippet_GetFileSasUri> //------------------------------------------------- // Create a SAS URI for a file //------------------------------------------------- public Uri GetFileSasUri(string shareName, string filePath, DateTime expiration, ShareFileSasPermissions permissions) { // Get the account details from app settings string accountName = ConfigurationManager.AppSettings["StorageAccountName"]; string accountKey = ConfigurationManager.AppSettings["StorageAccountKey"]; ShareSasBuilder fileSAS = new ShareSasBuilder() { ShareName = shareName, FilePath = filePath, // Specify an Azure file resource Resource = "f", // Expires in 24 hours ExpiresOn = expiration }; // Set the permissions for the SAS fileSAS.SetPermissions(permissions); // Create a SharedKeyCredential that we can use to sign the SAS token StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountName, accountKey); // Build a SAS URI UriBuilder fileSasUri = new UriBuilder($"https://{accountName}.file.core.windows.net/{fileSAS.ShareName}/{fileSAS.FilePath}"); fileSasUri.Query = fileSAS.ToSasQueryParameters(credential).ToString(); // Return the URI return(fileSasUri.Uri); }
public void FileSasBuilder_NullSharedKeyCredentialTest() { // Arrange var constants = TestConstants.Create(this); var shareName = GetNewShareName(); var filePath = GetNewDirectoryName(); ShareSasBuilder fileSasBuilder = BuildFileSasBuilder(includeVersion: true, includeFilePath: true, constants, shareName, filePath); // Act Assert.Throws <ArgumentNullException>(() => fileSasBuilder.ToSasQueryParameters(null), "sharedKeyCredential"); }
public SasQueryParameters GetNewFileServiceSasCredentialsShare(string shareName, StorageSharedKeyCredential sharedKeyCredentials = default) { var builder = new ShareSasBuilder { ShareName = shareName, Protocol = SasProtocol.None, StartsOn = Recording.UtcNow.AddHours(-1), ExpiresOn = Recording.UtcNow.AddHours(+1), IPRange = new SasIPRange(IPAddress.None, IPAddress.None) }; builder.SetPermissions(ShareSasPermissions.All); return(builder.ToSasQueryParameters(sharedKeyCredentials ?? GetNewSharedKeyCredentials())); }
public async Task SharePermissionsRawPermissions_Invalid() { // Arrange await using DisposingShare test = await GetTestShareAsync(); ShareSasBuilder blobSasBuilder = new ShareSasBuilder { StartsOn = Recording.UtcNow.AddHours(-1), ExpiresOn = Recording.UtcNow.AddHours(1), ShareName = test.Share.Name }; // Act TestHelper.AssertExpectedException( () => blobSasBuilder.SetPermissions( rawPermissions: "ptsdfsd", normalize: true), new ArgumentException("s is not a valid SAS permission")); }
public void FileSasBuilder_IdentifierTest() { // Arrange TestConstants constants = TestConstants.Create(this); string shareName = GetNewShareName(); string resource = "s"; ShareSasBuilder sasBuilder = new ShareSasBuilder { Identifier = constants.Sas.Identifier, ShareName = shareName, Resource = resource, Protocol = SasProtocol.Https, }; // Act SasQueryParameters sasQueryParameters = sasBuilder.ToSasQueryParameters(constants.Sas.SharedKeyCredential); // Assert Assert.AreEqual(constants.Sas.Identifier, sasQueryParameters.Identifier); Assert.AreEqual(resource, sasQueryParameters.Resource); Assert.AreEqual(SasProtocol.Https, sasQueryParameters.Protocol); Assert.AreEqual(SasQueryParametersInternals.DefaultSasVersionInternal, sasQueryParameters.Version); }