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 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")); }