Example #1
0
        public static string GetFileSasUri(string filePath, DateTime expiration, ShareFileSasPermissions permissions)
        {
            string shareName = MyWebUtils.Application.ToLower();

            // Get the account details from app settings
            string accountName = Properties.Settings.Default.StorageAccountName;
            string accountKey  = Properties.Settings.Default.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.ToString());
        }
        /// <summary>
        /// Create a permissions string to provide
        /// <see cref="ShareSasBuilder.Permissions"/>.
        /// </summary>
        /// <returns>A permissions string.</returns>
        internal static string ToPermissionsString(this ShareFileSasPermissions permissions)
        {
            var sb = new StringBuilder();

            if ((permissions & ShareFileSasPermissions.Read) == ShareFileSasPermissions.Read)
            {
                sb.Append(Constants.Sas.Permissions.Read);
            }
            if ((permissions & ShareFileSasPermissions.Create) == ShareFileSasPermissions.Create)
            {
                sb.Append(Constants.Sas.Permissions.Create);
            }
            if ((permissions & ShareFileSasPermissions.Write) == ShareFileSasPermissions.Write)
            {
                sb.Append(Constants.Sas.Permissions.Write);
            }
            if ((permissions & ShareFileSasPermissions.Delete) == ShareFileSasPermissions.Delete)
            {
                sb.Append(Constants.Sas.Permissions.Delete);
            }
            return(sb.ToString());
        }
Example #3
0
 /// <summary>
 /// Sets the permissions for a file SAS.
 /// </summary>
 /// <param name="permissions">
 /// <see cref="ShareFileSasPermissions"/> containing the allowed permissions.
 /// </param>
 public void SetPermissions(ShareFileSasPermissions permissions)
 {
     Permissions = permissions.ToPermissionsString();
 }
Example #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ShareSasBuilder"/>
 /// class to create a Blob Service Sas.
 /// </summary>
 /// <param name="permissions">
 /// The time at which the shared access signature becomes invalid.
 /// This field must be omitted if it has been specified in an
 /// associated stored access policy.
 /// </param>
 /// <param name="expiresOn">
 /// The time at which the shared access signature becomes invalid.
 /// This field must be omitted if it has been specified in an
 /// associated stored access policy.
 /// </param>
 public ShareSasBuilder(ShareFileSasPermissions permissions, DateTimeOffset expiresOn)
 {
     ExpiresOn = expiresOn;
     SetPermissions(permissions);
 }
Example #5
0
    public async Task <Uri> CreateShareFileSasUriAsync(string shareName, string filePath, ShareFileSasPermissions permissions, DateTimeOffset expiresOn)
    {
        if (string.IsNullOrWhiteSpace(shareName))
        {
            throw new ArgumentException($"'{nameof(shareName)}' cannot be null or whitespace.", nameof(shareName));
        }

        if (string.IsNullOrWhiteSpace(filePath))
        {
            throw new ArgumentException($"'{nameof(filePath)}' cannot be null or whitespace.", nameof(filePath));
        }

        var client = await CreateShareFileClientAsync(shareName, filePath, useSharedKey : true)
                     .ConfigureAwait(false);

        return(client.GenerateSasUri(permissions, expiresOn));
    }