public PolicyView(string name, SharedAccessPolicy policy) { PolicyName = name; Policy = policy; StartTime = Policy.SharedAccessStartTime.ToString(); ExpiryTime = Policy.SharedAccessExpiryTime.ToString(); }
/// <summary> /// Get the complete query builder for creating the Shared Access Signature query. /// </summary> /// <param name="policy">The shared access policy to hash.</param> /// <param name="groupPolicyIdentifier">An optional identifier for the policy.</param> /// <param name="resourceType">Either "b" for blobs or "c" for containers.</param> /// <param name="signature">The signature to use.</param> /// <returns>The finished query builder.</returns> internal static UriQueryBuilder GetShareAccessSignatureImpl( SharedAccessPolicy policy, string groupPolicyIdentifier, string resourceType, string signature) { CommonUtils.AssertNotNullOrEmpty("resourceType", resourceType); CommonUtils.AssertNotNull("signature", signature); UriQueryBuilder builder = new UriQueryBuilder(); // FUTURE blob for blob and container for container if (policy != null) { string permissions = SharedAccessPolicy.PermissionsToString(policy.Permissions); if (String.IsNullOrEmpty(permissions)) { permissions = null; } AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedStart, GetDateTimeOrNull(policy.SharedAccessStartTime)); AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedExpiry, GetDateTimeOrNull(policy.SharedAccessExpiryTime)); AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedPermissions, permissions); } builder.Add(Constants.QueryConstants.SignedResource, resourceType); AddEscapedIfNotNull(builder, Constants.QueryConstants.SignedIdentifier, groupPolicyIdentifier); AddEscapedIfNotNull(builder, Constants.QueryConstants.Signature, signature); return(builder); }
/// <summary> /// Get the signature hash embedded inside the Shared Access Signature. /// </summary> /// <param name="policy">The shared access policy to hash.</param> /// <param name="groupPolicyIdentifier">An optional identifier for the policy.</param> /// <param name="resourceName">The canonical resource string, unescaped.</param> /// <param name="client">The client whose credentials are to be used for signing.</param> /// <returns>The signed hash.</returns> internal static string GetSharedAccessSignatureHashImpl( SharedAccessPolicy policy, string groupPolicyIdentifier, string resourceName, CloudBlobClient client) { CommonUtils.AssertNotNull("policy", policy); CommonUtils.AssertNotNullOrEmpty("resourceName", resourceName); CommonUtils.AssertNotNull("client", client); ////StringToSign = signedpermissions + "\n" //// signedstart + "\n" //// signedexpiry + "\n" //// canonicalizedresource + "\n" //// signedidentifier ////HMAC-SHA256(URL.Decode(UTF8.Encode(StringToSign))) string stringToSign = string.Format( "{0}\n{1}\n{2}\n{3}\n{4}", SharedAccessPolicy.PermissionsToString(policy.Permissions), GetDateTimeOrEmpty(policy.SharedAccessStartTime), GetDateTimeOrEmpty(policy.SharedAccessExpiryTime), resourceName, groupPolicyIdentifier); string signature = client.Credentials.ComputeHmac(stringToSign); return(signature); }
public string GetSharedAccessSignature(string objId, DateTime expiryTime) { CloudBlob blob = this.container.GetBlobReference(objId); var blobAccess = new SharedAccessPolicy { Permissions = SharedAccessPermissions.Read, SharedAccessExpiryTime = expiryTime }; return(blob.Uri + blob.GetSharedAccessSignature(blobAccess)); }
////public string SharedAccessAuthenticatedURI(Uri uri, string permission) { return ""; } /// <summary> /// Writes a collection of shared access policies to the specified stream in XML format. /// </summary> /// <param name="sharedAccessPolicies">A collection of shared access policies.</param> /// <param name="outputStream">An output stream.</param> public static void WriteSharedAccessIdentifiers(SharedAccessPolicies sharedAccessPolicies, Stream outputStream) { CommonUtils.AssertNotNull("sharedAccessPolicies", sharedAccessPolicies); CommonUtils.AssertNotNull("outputStream", outputStream); if (sharedAccessPolicies.Count > Constants.MaxSharedAccessPolicyIdentifiers) { string errorMessage = string.Format( CultureInfo.CurrentCulture, SR.TooManyPolicyIdentifiers, sharedAccessPolicies.Count, Constants.MaxSharedAccessPolicyIdentifiers); throw new ArgumentOutOfRangeException("sharedAccessPolicies", errorMessage); } XmlWriterSettings settings = new XmlWriterSettings(); settings.Encoding = Encoding.UTF8; using (XmlWriter writer = XmlWriter.Create(outputStream, settings)) { writer.WriteStartElement(Constants.SignedIdentifiers); foreach (var key in sharedAccessPolicies.Keys) { writer.WriteStartElement(Constants.SignedIdentifier); // Set the identifier writer.WriteElementString(Constants.Id, key); // Set the permissions writer.WriteStartElement(Constants.AccessPolicy); var policy = sharedAccessPolicies[key]; writer.WriteElementString( Constants.Start, SharedAccessSignatureHelper.GetDateTimeOrEmpty(policy.SharedAccessStartTime)); writer.WriteElementString( Constants.Expiry, SharedAccessSignatureHelper.GetDateTimeOrEmpty(policy.SharedAccessExpiryTime)); writer.WriteElementString( Constants.Permission, SharedAccessPolicy.PermissionsToString(policy.Permissions)); writer.WriteEndElement(); writer.WriteEndElement(); } writer.WriteEndDocument(); } }
public static string GenerateSAS(string containerName, string blobUri) { string res = null; if ((string.IsNullOrEmpty(containerName)) || (string.IsNullOrEmpty(blobUri))) { throw new ArgumentException("Both container name and blob Uri must containt valid values"); } var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString"); var blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference(containerName); CloudBlob blob = blobClient.GetBlobReference(blobUri); container.CreateIfNotExist(); try { BlobContainerPermissions permissions = new BlobContainerPermissions(); // The container itself doesn't allow public access. permissions.PublicAccess = BlobContainerPublicAccessType.Off; // The container itself doesn't allow SAS access. SharedAccessPolicy containerPolicy = new SharedAccessPolicy() { Permissions = SharedAccessPermissions.None }; permissions.SharedAccessPolicies.Clear(); permissions.SharedAccessPolicies.Add("SASPolicy", containerPolicy); container.SetPermissions(permissions); // Generate an SAS for the blob. SharedAccessPolicy blobPolicy = new SharedAccessPolicy() { Permissions = SharedAccessPermissions.Read, SharedAccessExpiryTime = DateTime.UtcNow.AddDays(1d) }; res = blob.GetSharedAccessSignature(blobPolicy, "SASPolicy"); } catch (Exception ex) { Console.WriteLine(ex.Message); } return(res); }
public string GetSharedAccessSignature(string filename, int minutes) { SharedAccessPolicy sharedAccessPolicy = new SharedAccessPolicy(); sharedAccessPolicy.Permissions = SharedAccessPermissions.Read; sharedAccessPolicy.SharedAccessStartTime = DateTime.UtcNow; sharedAccessPolicy.SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(minutes); BlobContainerPermissions blobContainerPermissions = new BlobContainerPermissions(); blobContainerPermissions.SharedAccessPolicies.Add("default", sharedAccessPolicy); blobContainer.SetPermissions(blobContainerPermissions); CloudBlob blob = blobContainer.GetBlobReference(filename); var sas = blob.GetSharedAccessSignature(new SharedAccessPolicy(), "default"); return(blob.Uri.AbsoluteUri + sas); }
private static void SetReadOnlySharedAccessPolicy(CloudBlobContainer container) { var permissions = container.GetPermissions(); var options = new BlobRequestOptions { // Fail if someone else has already changed the container before we do. AccessCondition = AccessCondition.IfMatch(container.Properties.ETag) }; var sharedAccessPolicy = new SharedAccessPolicy { Permissions = SharedAccessPermissions.Read, SharedAccessExpiryTime = DateTime.UtcNow + TimeSpan.FromDays(StorageServicesContext.Current.Configuration.BlobsSasExpirationTime) }; permissions.SharedAccessPolicies.Remove("readonly"); permissions.SharedAccessPolicies.Add("readonly", sharedAccessPolicy); container.SetPermissions(permissions, options); }
// Generate a shared access signature for a policy. // Return true on success, false if not found, throw exception on error. public bool GenerateSharedAccessSignature(string containerName, SharedAccessPolicy policy, out string signature) { signature = null; try { CloudBlobContainer container = BlobClient.GetContainerReference(containerName); signature = container.GetSharedAccessSignature(policy); return(true); } catch (StorageClientException ex) { if ((int)ex.StatusCode == 404) { return(false); } throw; } }
private static void SetReadOnlySharedAccessPolicy(CloudBlobContainer container) { var blobSASExperiationTime = int.Parse(ConfigReader.GetConfigValue("BlobSASExperiationTime"), NumberStyles.Integer, CultureInfo.InvariantCulture); var permissions = container.GetPermissions(); var options = new BlobRequestOptions { // Fail if someone else has already changed the container before we do. AccessCondition = AccessCondition.IfMatch(container.Properties.ETag) }; var sharedAccessPolicy = new SharedAccessPolicy { Permissions = SharedAccessPermissions.Read, SharedAccessExpiryTime = DateTime.UtcNow + TimeSpan.FromDays(blobSASExperiationTime) }; permissions.SharedAccessPolicies.Remove("readonly"); permissions.SharedAccessPolicies.Add("readonly", sharedAccessPolicy); container.SetPermissions(permissions, options); }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) => { configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)); }); } ; CloudStorageAccount account = CloudStorageAccount.FromConfigurationSetting("dataConnStr"); CloudBlobClient blobClient = account.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference("testing12"); container.CreateIfNotExist(); CloudBlob blob = container.GetBlobReference("file.x"); SharedAccessPolicy containeraccess = new SharedAccessPolicy(); containeraccess.Permissions = SharedAccessPermissions.Read; SharedAccessPolicy blobaccess = new SharedAccessPolicy(); blobaccess.SharedAccessExpiryTime = DateTime.Now.AddDays(1); BlobContainerPermissions perm = new BlobContainerPermissions(); //perm.PublicAccess = BlobContainerPublicAccessType.Blob; perm.SharedAccessPolicies.Clear(); perm.SharedAccessPolicies.Add("test", containeraccess); container.SetPermissions(perm, new BlobRequestOptions()); HyperLink1.NavigateUrl = blob.Uri.AbsoluteUri + blob.GetSharedAccessSignature(blobaccess, "test"); }
public static SharedAccessAuthorizationRule Map(SharedAccessPolicy source) { if (source.SecondaryKey != null) { return(new SharedAccessAuthorizationRule( source.Name, source.PrimaryKey, source.SecondaryKey, source.AccessRights?.Select(Map) ?? Enumerable.Empty <AccessRights>())); } else if (source.PrimaryKey != null) { return(new SharedAccessAuthorizationRule( source.Name, source.PrimaryKey, source.AccessRights?.Select(Map) ?? Enumerable.Empty <AccessRights>())); } else { return(new SharedAccessAuthorizationRule( source.Name, source.AccessRights?.Select(Map) ?? Enumerable.Empty <AccessRights>())); } }
// Perform blob storage operations. static void TestBlobStorage() { string storageConnectionString = ConfigurationManager.ConnectionStrings["Storage"].ConnectionString; BlobHelper BlobHelper = new BlobHelper(storageConnectionString); try { Separator(); List <CloudBlobContainer> containers; Console.Write("List containers "); if (BlobHelper.ListContainers(out containers)) { Console.WriteLine("true"); if (containers != null) { foreach (CloudBlobContainer container in containers) { Console.Write(container.Name + " "); } Console.WriteLine(); } } else { Console.WriteLine("false"); } Separator(); Console.Write("Create container "); if (BlobHelper.CreateContainer("samplecontainer1")) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); Console.Write("Delete container "); if (BlobHelper.DeleteContainer("samplecontainer0")) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); Console.Write("Set container ACL "); if (BlobHelper.SetContainerACL("samplecontainer1", "container")) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); string accessLevel; Console.Write("Get container ACL "); if (BlobHelper.GetContainerACL("samplecontainer1", out accessLevel)) { Console.WriteLine("true " + accessLevel); } else { Console.WriteLine("false"); } Separator(); SortedList <string, SharedAccessPolicy> policies = new SortedList <string, SharedAccessPolicy>(); SharedAccessPolicy policy1 = new SharedAccessPolicy() { Permissions = SharedAccessPermissions.List | SharedAccessPermissions.Read | SharedAccessPermissions.Write | SharedAccessPermissions.Delete, SharedAccessStartTime = DateTime.UtcNow, SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1) }; policies.Add("Policy1", policy1); policies.Add("Policy2", new SharedAccessPolicy() { Permissions = SharedAccessPermissions.Read, SharedAccessStartTime = DateTime.Parse("2010-01-01T09:38:05Z"), SharedAccessExpiryTime = DateTime.Parse("2012-12-31T09:38:05Z") }); Console.Write("Set container access policy "); if (BlobHelper.SetContainerAccessPolicy("samplecontainer1", policies)) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); Console.Write("Get container access policy "); if (BlobHelper.GetContainerAccessPolicy("samplecontainer1", out policies)) { Console.WriteLine("true"); if (policies != null) { foreach (KeyValuePair <string, SharedAccessPolicy> policy in policies) { Console.WriteLine("Policy " + policy.Key); } } } else { Console.WriteLine("false"); } Separator(); string signature = String.Empty; Console.Write("Create shared access signature "); if (BlobHelper.GenerateSharedAccessSignature("samplecontainer1", policy1, out signature)) { Console.WriteLine("true " + signature); } else { Console.WriteLine("false"); } Separator(); signature = String.Empty; Console.Write("Create shared access signature from access policy "); if (BlobHelper.GenerateSharedAccessSignature("samplecontainer1", "Policy1", out signature)) { Console.WriteLine("true " + signature); } else { Console.WriteLine("false"); } Separator(); signature = String.Empty; Console.Write("Create shared access signature from access policy 2 "); if (BlobHelper.GenerateSharedAccessSignature("samplecontainer1", "Policy2", out signature)) { Console.WriteLine("true " + signature); } else { Console.WriteLine("false"); } Separator(); BlobContainerProperties containerProperties = new BlobContainerProperties(); Console.Write("Get container properties "); if (BlobHelper.GetContainerProperties("samplecontainer1", out containerProperties)) { Console.WriteLine("true"); Console.WriteLine("BlobContainerProperties.Etag: " + containerProperties.ETag); Console.WriteLine("BlobContainerProperties.LastModifiedUtc: " + containerProperties.LastModifiedUtc.ToShortDateString()); } else { Console.WriteLine("false"); } Separator(); NameValueCollection metadata = new NameValueCollection(); metadata.Add("property1", "Value1"); metadata.Add("property2", "Value2"); metadata.Add("property3", "Value3"); Console.Write("Set container metadata "); if (BlobHelper.SetContainerMetadata("samplecontainer1", metadata)) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); metadata = new NameValueCollection(); Console.Write("Get container metadata "); if (BlobHelper.GetContainerMetadata("samplecontainer1", out metadata)) { Console.WriteLine("true"); if (metadata != null) { for (int i = 0; i < metadata.Count; i++) { Console.WriteLine(metadata.GetKey(i) + ": " + metadata.Get(i)); } } } else { Console.WriteLine("false"); } Separator(); //Console.Write("Delete container container1 "); //if (BlobHelper.DeleteContainer("samplecontainer1")) //{ // Console.WriteLine("true"); //} //else //{ // Console.WriteLine("false"); //} Separator(); List <CloudBlob> blobs; Console.Write("List blobs "); if (BlobHelper.ListBlobs("samplecontainer1", out blobs)) { Console.WriteLine("true"); if (blobs != null) { foreach (CloudBlob blob in blobs) { Console.WriteLine(blob.Uri.LocalPath); } } } else { Console.WriteLine("false"); } Separator(); Console.Write("Put blob "); if (BlobHelper.PutBlob("samplecontainer1", "blob1.txt", "This is a text blob!")) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); Console.Write("Put page blob "); if (BlobHelper.PutBlob("samplecontainer1", "pageblob1.txt", 2048)) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); string page0 = new string('A', 512); Console.Write("Put page 0 "); if (BlobHelper.PutPage("samplecontainer1", "pageblob1.txt", page0, 0, 512)) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); string page1 = new string('B', 512); Console.Write("Put page 1 "); if (BlobHelper.PutPage("samplecontainer1", "pageblob1.txt", page1, 512, 512)) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); page0 = String.Empty; Console.Write("Get page 0 "); if (BlobHelper.GetPage("samplecontainer1", "pageblob1.txt", 0, 512, out page0)) { Console.WriteLine("true"); Console.WriteLine(page0); } else { Console.WriteLine("false"); } Separator(); page1 = String.Empty; Console.Write("Get page 1 "); if (BlobHelper.GetPage("samplecontainer1", "pageblob1.txt", 512, 512, out page1)) { Console.WriteLine("true"); Console.WriteLine(page1); } else { Console.WriteLine("false"); } Separator(); PageRange[] ranges; Console.Write("Get page regions "); if (BlobHelper.GetPageRegions("samplecontainer1", "pageblob1.txt", out ranges)) { Console.WriteLine("true"); if (ranges != null) { foreach (PageRange range in ranges) { Console.WriteLine(range.StartOffset.ToString() + "-" + range.EndOffset.ToString()); } } } else { Console.WriteLine("false"); } Separator(); string content; Console.Write("Get blob "); if (BlobHelper.GetBlob("samplecontainer1", "blob1.txt", out content)) { Console.WriteLine("true"); Console.WriteLine(content); } else { Console.WriteLine("false"); } Separator(); Console.Write("Copy blob "); if (BlobHelper.CopyBlob("samplecontainer1", "blob1.txt", "samplecontainer1", "blob2.txt")) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); Console.Write("Snapshot blob "); if (BlobHelper.SnapshotBlob("samplecontainer1", "blob1.txt")) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); Console.Write("Delete blob "); if (BlobHelper.DeleteBlob("samplecontainer1", "blob2.txt")) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); SortedList <string, string> properties = new SortedList <string, string>(); properties.Add("ContentType", "text/html"); Console.Write("Set blob properties "); if (BlobHelper.SetBlobProperties("samplecontainer1", "blob1.txt", properties)) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); Console.Write("Get blob properties "); if (BlobHelper.GetBlobProperties("samplecontainer1", "blob1.txt", out properties)) { Console.WriteLine("true"); foreach (KeyValuePair <string, string> item in properties) { Console.WriteLine(item.Key + ": " + item.Value); } } else { Console.WriteLine("false"); } Separator(); Console.Write("Put blob If Unchanged 1 "); if (BlobHelper.PutBlobIfUnchanged("samplecontainer1", "blob1.txt", "This is a text blob!", properties["ETag"])) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); Console.Write("Put blob If Unchanged 2 "); if (BlobHelper.PutBlobIfUnchanged("samplecontainer1", "blob1.txt", "This is a text blob!", "BadETag")) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); Console.Write("Put blob with MD5 "); if (BlobHelper.PutBlobWithMD5("samplecontainer1", "blob1.txt", "This is a text blob!")) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); metadata.Clear(); metadata.Add("property1", "Value1"); metadata.Add("property2", "Value2"); metadata.Add("property3", "Value3"); metadata.Add("property4", "Value4"); Console.Write("Set blob metadata "); if (BlobHelper.SetBlobMetadata("samplecontainer1", "blob1.txt", metadata)) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); Console.Write("Get blob metadata "); if (BlobHelper.GetBlobMetadata("samplecontainer1", "blob1.txt", out metadata)) { Console.WriteLine("true"); for (int i = 0; i < metadata.Count; i++) { Console.WriteLine(metadata.GetKey(i) + ": " + metadata.Get(i)); } } else { Console.WriteLine("false"); } Separator(); string leaseAction = "acquire"; string leaseId = null; Console.Write("Lease blob - acquire "); if (BlobHelper.LeaseBlob("samplecontainer1", "blob1.txt", leaseAction, ref leaseId)) { Console.WriteLine("true " + leaseId); } else { Console.WriteLine("false"); } Separator(); leaseAction = "release"; Console.Write("Lease blob - release "); if (BlobHelper.LeaseBlob("samplecontainer1", "blob1.txt", leaseAction, ref leaseId)) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); string[] blockIds = new string[3]; Console.Write("Put block 0 "); if (BlobHelper.PutBlock("samplecontainer1", "largeblob1.txt", 0, blockIds, "AAAAAAAAAA")) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); Console.Write("Put block 1 "); if (BlobHelper.PutBlock("samplecontainer1", "largeblob1.txt", 1, blockIds, "BBBBBBBBBB")) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); Console.Write("Put block 2 "); if (BlobHelper.PutBlock("samplecontainer1", "largeblob1.txt", 2, blockIds, "CCCCCCCCCC")) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); string[] getblockids = null; Console.Write("Get block list "); if (BlobHelper.GetBlockList("samplecontainer1", "largeblob1.txt", out getblockids)) { Console.WriteLine("true"); if (getblockids != null) { for (int i = 0; i < getblockids.Length; i++) { Console.WriteLine("Block " + i + " id = " + getblockids[i]); } } } else { Console.WriteLine("false"); } Separator(); Console.Write("Put block list "); if (BlobHelper.PutBlockList("samplecontainer1", "largeblob1.txt", blockIds)) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } } catch (Exception ex) { Console.WriteLine("EXCEPTION " + ex.ToString()); } }
public Expense GetExpenseById(string username, string expenseId) { var context = new ExpenseDataContext(this.account) { MergeOption = MergeOption.NoTracking }; string expenseRowKey = KeyGenerator.ExpenseEntityRowKey(expenseId); char charAfterSeparator = Convert.ToChar((Convert.ToInt32('_') + 1)); var nextExpenseRowId = expenseRowKey + charAfterSeparator; string expenseItemRowKey = string.Format(CultureInfo.InvariantCulture, "{0}{1}", ExpenseItemEntity.RowKeyPrefix, expenseId); var nextExpenseItemRowId = expenseItemRowKey + charAfterSeparator; // TODO: Update to only have to compare to the expense id key and not the entire row var expenseQuery = (from e in context.ExpenseExpenseItem where e.PartitionKey == username.EncodePartitionAndRowKey() && ((e.RowKey.CompareTo(expenseRowKey) >= 0 && e.RowKey.CompareTo(nextExpenseRowId) < 0) || (e.RowKey.CompareTo(expenseItemRowKey) >= 0 && e.RowKey.CompareTo(nextExpenseItemRowId) < 0)) select e).AsTableServiceQuery(); Expense expense = null; var items = new List <ExpenseItem>(); try { foreach (var entity in expenseQuery.Execute()) { switch (entity.ToEnum <TableKinds>()) { case TableKinds.Expense: expense = entity.ToKind <IExpenseEntity>().ToModel(); break; case TableKinds.ExpenseItem: items.Add(entity.ToKind <IExpenseItemEntity>().ToModel()); break; } } } catch (InvalidOperationException e) { Log.Write(EventKind.Error, e.Message); throw; } if (expense == null) { return(expense); } items.ForEach(x => expense.Details.Add(x)); var policy = new SharedAccessPolicy { Permissions = SharedAccessPermissions.Read, SharedAccessExpiryTime = DateTime.UtcNow + this.sharedSignatureValiditySpan }; var client = this.account.CreateCloudBlobClient(); var container = client.GetContainerReference(AzureStorageNames.ReceiptContainerName); foreach (var item in expense.Details) { if (item.ReceiptUrl != null) { CloudBlob receiptBlob = container.GetBlobReference(item.ReceiptUrl.ToString()); item.ReceiptUrl = new Uri(item.ReceiptUrl.AbsoluteUri + receiptBlob.GetSharedAccessSignature(policy)); } else { item.ReceiptUrl = new Uri("/Styling/Images/no_receipt.png", UriKind.Relative); } if (item.ReceiptThumbnailUrl != null) { CloudBlob receiptThumbnailBlob = container.GetBlobReference(item.ReceiptThumbnailUrl.ToString()); item.ReceiptThumbnailUrl = new Uri(item.ReceiptThumbnailUrl.AbsoluteUri + receiptThumbnailBlob.GetSharedAccessSignature(policy)); } else { item.ReceiptThumbnailUrl = new Uri("/Styling/Images/no_receipt.png", UriKind.Relative); } } return(expense); }