在Xamarin

时间:2017-03-09 07:48:31

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

我是一个小型物联网项目,我正在逐渐减少,它基本上只是抓住了我最新的传感器遥测技术#34; smart"在一个非常标准的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来检索相同的值,所有这一切都会消失,但这只是从存储表中获取值,而我真的不会这样做。 t看到需要为它创建一个inbetween 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>

1 个答案:

答案 0 :(得分:2)

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

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

  

var sas =   ?” SV = 2016年5月31日&安培; SS = bfqt&安培; SRT = SCO&安培; SP = rwdlacup&安培; SE = 2019-03-08T23:51:51Z&安培; ST = 2017-03-08T15:51:51Z&安培; SPR = HTTPS&安培; SIG = 0000000000000000000000000000000000000000000000" ;

试试这个。