亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Cosmos DB Azure 表 API oData 身份驗證 REST/C#?

Cosmos DB Azure 表 API oData 身份驗證 REST/C#?

C#
桃花長相依 2021-10-09 10:22:49
我正在嘗試使用 Table API 訪問 Azure Cosmos DB。挑戰是,盡管創建了 SharedKeyLite,服務器仍然返回 Unauthorized - 似乎 SharedKeyLite 不受支持,或者我生成的簽名或標頭錯誤。這是代碼    static readonly string storageAccountName = "accountName";    static readonly string storageAccountKey = "xxxx";    static readonly string uri = "https://accountName.table.cosmosdb.azure.com/Contacts()";    static readonly string utc_date = DateTime.UtcNow.ToString("r");    static void Main(string[] args)    {        Console.WriteLine(GetResult().Result);    }    static async Task<string> GetResult()    {        // Set this to whatever payload you desire. Ours is null because         //   we're not passing anything in.        Byte[] requestPayload = null;        var requestDateString = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);        var requestUri = new Uri(uri);        DateTime now = DateTime.UtcNow;        //Instantiate the request message with a null payload.        using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, uri)        { Content = (requestPayload == null) ? null : new ByteArrayContent(requestPayload) })        {            ConstructHeaders(httpRequestMessage.Headers, requestDateString);            string authorizationHeader = GenerateSharedKeyLite(storageAccountKey, storageAccountName, uri,requestDateString);            httpRequestMessage.Headers.Authorization = new AuthenticationHeaderValue("SharedKeyLite", authorizationHeader);            // Send the request.            using (HttpResponseMessage httpResponseMessage = await new HttpClient().SendAsync(httpRequestMessage))            {                string json = await httpResponseMessage.Content.ReadAsStringAsync();                return json;            }        }    }這些是我添加的標題,ConstructHeaders 方法的擴展。請參閱此鏈接以獲取請求參數
查看完整描述

1 回答

?
慕絲7291255

TA貢獻1859條經驗 獲得超6個贊

只需復制您的代碼,它就可以在我身邊工作。如果您還沒有修改您的代碼,請確保您的storageAccountNamestorageAccountKey是正確的。

順便說一句,在方法中GenerateSharedKeyLite不需要canonicalizedResourceString為實體操作添加查詢參數。comp如果要操作表或服務的組件信息,則只需添加。請參閱構建規范化資源字符串。

查詢字符串應包括問號和comp參數(例如,?comp=metadata)。查詢字符串中不應包含其他參數。


查看完整回答
反對 回復 2021-10-09
  • 1 回答
  • 0 關注
  • 223 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號