C# 开发存储帐户需要身份验证
我正在使用CloudTableClient访问表存储:C# 开发存储帐户需要身份验证,c#,azure,.net-core,azure-table-storage,C#,Azure,.net Core,Azure Table Storage,我正在使用CloudTableClient访问表存储: private StorageCredentials credentials; public StorageCredentials StorageAccount { get { if (credentials == null) { credentials = new StorageCredentials(config["AzureStorageSettings:AccountNa
private StorageCredentials credentials;
public StorageCredentials StorageAccount
{
get
{
if (credentials == null)
{
credentials = new StorageCredentials(config["AzureStorageSettings:AccountName"],
config["AzureStorageSettings:AccountKey"]);
}
return credentials;
}
}
public CloudTableClient Tableclient
{
get
{
var storageAccount = new CloudStorageAccount(StorageAccount, true);
return storageAccount.CreateCloudTableClient();
}
}
public string TableReference => config["CampaignStorageName"];
使用以下设置:
"AzureStorageSettings:AccountName": "devstoreaccount1",
"AzureStorageSettings:AccountKey": "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==",
但每次我试图从表中检索记录时
public IngestionEntity GetIngestionRecord(IngestionMessage msg)
{
var table = Tableclient.GetTableReference(TableReference);
var retrieve = TableOperation.Retrieve<IngestionEntity>(msg.PartitionKey, msg.RowKey);
var result = table.Execute(retrieve);
log.LogInformation($"{msg.ToString()}, " +
$"Query Status Code: {result.HttpStatusCode}, " +
$"Cost: {result.RequestCharge}");
return (IngestionEntity)result.Result;
}
我假设这与开发人员存储使用的https v http有关,但是否有一种方法允许此验证,以便我可以如上所述使用TableClient,或者我应该以不同的方式进行此操作?请尝试以下代码:
private CloudStorageAccount storageAccount;
public CloudStorageAccount StorageAccount
{
get
{
if (storageAccount == null)
{
storageAccount = config["AzureStorageSettings:AccountName"] == "devstoreaccount1" ? CloudStorageAccount.DevelopmentStorageAccount : new CloudStorageAccount(new StorageCredentials(config["AzureStorageSettings:AccountName"], config["AzureStorageSettings:AccountKey"]), true);
}
return storageAccount;
}
}
public CloudTableClient Tableclient
{
get
{
return StorageAccount.CreateCloudTableClient();
}
}
基本上,Storage Emulator的端点与常规存储帐户不同,当您使用Storage Emulator凭据创建
CloudStorageAccount
的实例时,SDK会认为您正在尝试连接到云中名为devstoreaccount1
的帐户。因为devstoreaccount1
的密钥与Storage Emulator帐户的密钥不同,所以您将得到403错误。结果与此相同,但我明白了这一点,我会看看我能得到什么样的结果。如果我检查凭据,则没有SAS或签名。在更改其他设置后,尝试了此凭据=配置[“ASPNETCORE_环境”]=“Development”,并且使用开发人员凭据进行了正确设置,但仍然产生错误。我犯了一个错误,对此表示抱歉!更新了我的答案。实际上,您需要更改您的StorageAccount
属性,使其类型为CloudStorageAccount
,而不是StorageCredentials
。非常感谢。我必须默认私有CloudStorageAccount-storageAccount=null;因为它在默认情况下似乎不为null。我还改为使用ASPNETCORE_环境配置值来实现我所追求的目标,但现在它从dev storageAwesome中检索!我很高兴听到你的消息。
private CloudStorageAccount storageAccount;
public CloudStorageAccount StorageAccount
{
get
{
if (storageAccount == null)
{
storageAccount = config["AzureStorageSettings:AccountName"] == "devstoreaccount1" ? CloudStorageAccount.DevelopmentStorageAccount : new CloudStorageAccount(new StorageCredentials(config["AzureStorageSettings:AccountName"], config["AzureStorageSettings:AccountKey"]), true);
}
return storageAccount;
}
}
public CloudTableClient Tableclient
{
get
{
return StorageAccount.CreateCloudTableClient();
}
}