【发布时间】:2018-07-27 14:10:34
【问题描述】:
我正在尝试使用只读 SAS 密钥连接到 Azure CloudTable 并进行查询。我在 Microsoft Azure Storage Explorer 中使用了完全相同的 SAS,完全没有问题。
我尝试了几种方法(来自这个网站),但我总是得到 403 Forbidden:
方法一:
string _SAS = "https://[myaccount].table.core.windows.net/?sv=2017-07-29&ss=bfqt&srt=sco&sp=rlp&se=2019-02-22T03:14:04Z&st=2018-02-21T19:14:04Z&spr=https&sig=[mySignature]";
StorageCredentials accountSAS = new StorageCredentials(_devSAS);
CloudStorageAccount storageAccount = new CloudStorageAccount(accountSAS, "[MyAccount]", endpointSuffix: null, useHttps: true);
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable tableClientTable = tableClient.GetTableReference("[MyTableName]");
TableQuery<AccessErrorLog> rangeQuery = new TableQuery<AccessErrorLog>().Where(
TableQuery.CombineFilters(
TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, AspNetCompaniesId),
TableOperators.And,
TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.GreaterThanOrEqual, date)
),
TableOperators.And,
TableQuery.GenerateFilterCondition("ProgramSource", QueryComparisons.Equal, ProgramSource)
)
);
foreach (AccessErrorLog entity in tableClientTable.ExecuteQuery(rangeQuery))
{
LogInformationRecords.Add(entity.ErrorMessage);
}
方法二:
StorageCredentials accountSAS = new StorageCredentials(_devSAS);
CloudTable tableClientTable = new CloudTable(new Uri("https://[MyAccount].table.core.windows.net/[MyTableName]"), accountSAS);
// Same query and foreach as above...
如果我使用实际的连接字符串(即string _devKey = "DefaultEndpointsProtocol=https;AccountName=[MyAccount];AccountKey=[MyAccountKey]"; 并使用CloudStorageAccount.Parse(_devKey);,则查询和执行工作正常,不会出现 403 错误。
因为我可以在 Microsoft Azure 存储资源管理器中毫无问题地使用 SAS,并且我可以在我的应用程序中使用实际的连接字符串而没有任何问题,所以我只能认为我遗漏了一些非常明显的东西。
编辑:当我在 Azure 存储资源管理器中使用它时,我忘了提及 SAS 声称拥有“读取、列出、处理”权限。
感谢任何帮助,谢谢!
【问题讨论】:
标签: c# azure azure-storage http-status-code-403