开发存储帐户需要身份验证

时间:2020-03-25 10:33:39

标签: c# azure .net-core azure-table-storage

我正在使用CloudTableClient访问表存储:

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;
}

我收到以下错误:

 Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.

我假设这与开发存储使用的https v http有关,但是是否有一种方法可以对其进行身份验证,以便可以像上面那样使用TableClient或应该以其他方式进行操作?

1 个答案:

答案 0 :(得分:2)

请尝试以下代码:

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 Accounts不同的终结点,并且当您使用Storage Emulator凭据创建CloudStorageAccount的实例时,SDK认为您正在尝试连接到名为devstoreaccount1的帐户在云中。由于devstoreaccount1的密钥与Storage Emulator帐户的密钥不同,因此会出现403错误。