예제 #1
0
        /// <summary>
        /// Validate the file share access policy
        /// </summary>
        /// <param name="policy">SharedAccessFilePolicy object</param>
        /// <param name="policyIdentifier">The policy identifier which need to be checked.</param>
        public static bool ValidateShareAccessPolicy(IStorageFileManagement channel, string shareName,
                                                     string policyIdentifier, bool shouldNoPermission, bool shouldNoStartTime, bool shouldNoExpiryTime)
        {
            if (string.IsNullOrEmpty(policyIdentifier))
            {
                return(true);
            }
            CloudFileShare       fileShare = channel.GetShareReference(shareName);
            FileSharePermissions permission;

            try
            {
                permission = fileShare.GetPermissionsAsync().Result;
            }
            catch (AggregateException e) when(e.InnerException is StorageException)
            {
                throw e.InnerException;
            }

            SharedAccessFilePolicy sharedAccessPolicy =
                GetExistingPolicy <SharedAccessFilePolicy>(permission.SharedAccessPolicies, policyIdentifier);

            if (shouldNoPermission && sharedAccessPolicy.Permissions != SharedAccessFilePermissions.None)
            {
                throw new InvalidOperationException(Resources.SignedPermissionsMustBeOmitted);
            }

            if (shouldNoStartTime && sharedAccessPolicy.SharedAccessStartTime.HasValue)
            {
                throw new InvalidOperationException(Resources.SignedStartTimeMustBeOmitted);
            }

            if (shouldNoExpiryTime && sharedAccessPolicy.SharedAccessExpiryTime.HasValue)
            {
                throw new InvalidOperationException(Resources.SignedExpiryTimeMustBeOmitted);
            }

            return(!sharedAccessPolicy.SharedAccessExpiryTime.HasValue);
        }
예제 #2
0
 public Task <FileSharePermissions> GetSharePermissionsAsync(CloudFileShare share, AccessCondition accessCondition,
                                                             FileRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken)
 {
     return(share.GetPermissionsAsync(accessCondition, options, operationContext, cancellationToken));
 }
 public FileSharePermissions GetSharePermissions(CloudFileShare share, AccessCondition accessCondition = null,
                                                 FileRequestOptions options = null, OperationContext operationContext = null)
 {
     return(share.GetPermissionsAsync(accessCondition, options, operationContext).Result);
 }
 public static FileSharePermissions GetPermissions(this CloudFileShare share, AccessCondition accessCondition = null, FileRequestOptions options = null, OperationContext operationContext = null)
 {
     return(share.GetPermissionsAsync(accessCondition, options, operationContext).GetAwaiter().GetResult());
 }
예제 #5
0
        public void WriteFilesIntoFileService()
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_configuration.ReadKeyFromFilePath(Constants.ConnectionStringPathKey));

            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

            CloudFileShare fileShare = fileClient.GetShareReference("h2h");

            if (Task.Run(async() => await fileShare.ExistsAsync()).Result)
            {
                string policyName = "DemoPolicy" + new Random().Next(50);

                FileSharePermissions fileSharePermissions = Task.Run(async() => await fileShare.GetPermissionsAsync()).Result;

                // define policy
                SharedAccessFilePolicy sharedAccessFilePolicy = new SharedAccessFilePolicy()
                {
                    SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1),
                    //Permissions = SharedAccessFilePermissions.Read
                    Permissions = SharedAccessFilePermissions.Write
                };

                fileSharePermissions.SharedAccessPolicies.Add(policyName, sharedAccessFilePolicy);

                // set permissions of file share
                Task.Run(async() => await fileShare.SetPermissionsAsync(fileSharePermissions));

                // generate SAS token based on policy and use to create a new file
                CloudFileDirectory rootDirectory = fileShare.GetRootDirectoryReference();

                if (Task.Run(async() => await rootDirectory.ExistsAsync()).Result)
                {
                    CloudFileDirectory customDirectory = rootDirectory.GetDirectoryReference("Output");
                    if (Task.Run(async() => await customDirectory.ExistsAsync()).Result)
                    {
                        CloudFile file     = customDirectory.GetFileReference(_globalNotesPdf.Name);
                        string    sasToken = file.GetSharedAccessSignature(null, policyName);

                        //generate URL of file with SAS token
                        Uri       fileSASUrl = new Uri(file.StorageUri.PrimaryUri.ToString() + sasToken);
                        CloudFile newFile    = new CloudFile(fileSASUrl);

                        Task.Run(async() => await newFile.UploadFromFileAsync(_globalNotesPdf.FullName));
                    }
                }
            }
        }
예제 #6
0
        public void UploadFileStream(Stream stream, string connectionString, FileInfo outputFile)
        {
            connectionString.ThrowExceptionIfNullOrEmpty();

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

            CloudFileShare fileShare = fileClient.GetShareReference("h2h");

            if (Task.Run(async() => await fileShare.ExistsAsync()).Result)
            {
                string policyName = "DemoPolicy" + new Random().Next(50);

                FileSharePermissions fileSharePermissions = Task.Run(async() => await fileShare.GetPermissionsAsync()).Result;

                // define policy
                SharedAccessFilePolicy sharedAccessFilePolicy = new SharedAccessFilePolicy()
                {
                    SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1),
                    //Permissions = SharedAccessFilePermissions.Read
                    Permissions = SharedAccessFilePermissions.Write
                };

                fileSharePermissions.SharedAccessPolicies.Add(policyName, sharedAccessFilePolicy);

                // set permissions of file share
                Task.Run(async() => await fileShare.SetPermissionsAsync(fileSharePermissions));

                // generate SAS token based on policy and use to create a new file
                CloudFileDirectory rootDirectory = fileShare.GetRootDirectoryReference();

                if (Task.Run(async() => await rootDirectory.ExistsAsync()).Result)
                {
                    CloudFileDirectory customDirectory = rootDirectory.GetDirectoryReference("HyperArchive");
                    if (Task.Run(async() => await customDirectory.ExistsAsync()).Result)
                    {
                        CloudFile file     = customDirectory.GetFileReference(outputFile.Name);
                        string    sasToken = file.GetSharedAccessSignature(null, policyName);

                        //generate URL of file with SAS token
                        Uri       fileSASUrl = new Uri(file.StorageUri.PrimaryUri.ToString() + sasToken);
                        CloudFile newFile    = new CloudFile(fileSASUrl);

                        var taskResult = Task.Run(async() => await newFile.UploadFromStreamAsync(stream));
                    }
                }
            }
        }