【问题标题】:Connecting to Azure Storage Tables in Xamarin在 Xamarin 中连接到 Azure 存储表
【发布时间】:2017-03-09 07:48:31
【问题描述】:

我有一个小型物联网项目,我正在减少它,它基本上只是从我的“智能”房屋中获取我最新的传感器遥测数据,并将其显示在一个非常标准的 Xamarin 应用程序中的表单上。

我的问题是关于 Xamarin 和 Azure.Storage 的使用:

我在我的应用中下载了 3 个表格:

public DeviceReadingsReader()
{
    _deviceReadingsTable = TableClient.GetTableReference("DeviceReadings");
    _locationsTable = TableClient.GetTableReference("Locations");
    _goalsTable = TableClient.GetTableReference("TemperatureGoals");
}

如您所知,它们都引用了一个 TableClient,我发现它使用的是与 PCL 兼容的旧 NuGet 包(一个问题:为什么不能安装最新的 azure.storage 包Xamarin.Forms??)

在 UWP 和 iOS 上,我这样做没有问题:

    var cloudStorageAccount = CloudStorageAccount.Parse(connectionstring);
    _tableClient = storageAccount.CreateCloudTableClient();

但是,当我在 Android 上运行此代码时,我收到一个异常,要求我改用 SAS 令牌,因此我在 Azure 门户中为存储帐户创建了一个 SaS 令牌,有效期为 2 年,并使用它像这样:

但是,在 Android、iOS 或 UWP 上运行它会给我一条授权失败消息,表明我的 SaS 令牌无效。

有谁知道我做错了什么,以及如何编写一段可以在所有 3 个平台上实际执行的代码?

var sas = "https://0000000000000.table.core.windows.net/?sv=2016-05-31&ss=bfqt&srt=sco&sp=rwdlacup&se=2019-03-08T23:51:51Z&st=2017-03-08T15:51:51Z&spr=https&sig=0000000000000000000000000000000000000000000000&comp=list&restype=table";
StorageCredentials creds = new StorageCredentials(sas);
CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(creds, null, null, new Uri("https://0000000000000.table.core.windows.net/"), null);
_tableClient = cloudStorageAccount.CreateCloudTableClient();           

编辑:我知道我可以创建一个云 API 来检索相同的值,所有这些都会消失,但这只是从存储表中获取值,我真的不认为有必要为它创建一个中间 WebApi

这是一个例外:

<RequestResult>
  <HTTPStatusCode>403</HTTPStatusCode>
  <HttpStatusMessage>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.</HttpStatusMessage>
  <TargetLocation>Primary</TargetLocation>
  <ServiceRequestID>926f8520-0002-0033-02b2-98bab7000000</ServiceRequestID>
  <ContentMd5 />
  <Etag />
  <RequestDate>Thu, 09 Mar 2017 09:55:29 GMT</RequestDate>
  <StartTime>Thu, 09 Mar 2017 08:55:28 GMT</StartTime>
  <EndTime>Thu, 09 Mar 2017 08:55:30 GMT</EndTime>
  <Error>
    <Code>AuthenticationFailed</Code>
    <Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:926f8520-0002-0033-02b2-98bab7000000
Time:2017-03-09T08:55:32.7622395Z</Message>
  </Error>
  <ExceptionInfo>
    <Type />
    <HResult>-2147467259</HResult>
    <Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.</Message>
    <Source>Microsoft.WindowsAzure.Storage</Source>
    <StackTrace>  at Microsoft.WindowsAzure.Storage.Core.Executor.Executor+&lt;ExecuteAsyncInternal&gt;d__0`1[T].MoveNext () [0x007eb] in &lt;03db1448eaed4d018343fcf0153c030d&gt;:0 </StackTrace>
  </ExceptionInfo>
</RequestResult>

【问题讨论】:

  • 您能分享一下您的 SAS 令牌是什么样的吗?只需混淆 SAS 中的 sig 部分。另外请分享您收到授权失败错误的代码。
  • 重写了原始帖子以包含 SAS 令牌和异常消息。我已经尝试过使用 SAS 令牌的变体无济于事(包括/排除 URL,添加 restype 作为另一篇文章的提示也没有帮助

标签: c# azure xamarin azure-table-storage


【解决方案1】:

因此,您使用 SAS 的方式存在问题。您正在使用 SAS URL。请仅使用查询字符串部分:

所以你的 SAS 应该是这样的:

var sas = "?sv=2016-05-31&ss=bfqt&srt=sco&sp=rwdlacup&se=2019-03-08T23:51:51Z&st=2017-03-08T15:51:51Z&spr=https&sig=00000000000000000000000000000000000

试试这个。

【讨论】:

    猜你喜欢
    • 2017-12-30
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    相关资源
    最近更新 更多