【问题标题】:Azure.DocumentDb The MAC signature found in the HTTP request is not the same as the computed signatureAzure.DocumentDb HTTP 请求中找到的 MAC 签名与计算的签名不同
【发布时间】:2019-01-16 13:50:00
【问题描述】:

我有一个使用 azure cosmosdb 的课程。我的班级是这样的:

public class DocumentService : IDocumentService
{ 
    private readonly DocumentClient _client;
    private readonly string _collectionName;
    private readonly string _databaseName;

    public DocumentService(IDocumentDbConfig settings)
    {
        var connectionPolicy = new ConnectionPolicy
        {
            ConnectionMode = ConnectionMode.Direct,
            ConnectionProtocol = Protocol.Tcp,
            RequestTimeout = new TimeSpan(1, 0, 0),
            MaxConnectionLimit = 1000,
            RetryOptions = new RetryOptions
            {
                MaxRetryAttemptsOnThrottledRequests = 10,
                MaxRetryWaitTimeInSeconds = 60
            }
        };

        _databaseName = settings.DocumentDbDatabaseName;
        _collectionName = settings.DocumentDbProductsCollectionName;
        _client = new DocumentClient(new Uri(settings.DocumentDbEnpointUrl), settings.DocumentDbPrimaryKey, connectionPolicy);
    }

    public IList<JObject> List(string query = "SELECT * FROM c") => _client.CreateDocumentQuery<JObject>(UriFactory.CreateDocumentCollectionUri(_databaseName, _collectionName), query, new FeedOptions {EnableCrossPartitionQuery = true}).AsEnumerable().ToList();

    public async Task SaveAsync(IEnumerable<JObject> models)
    {
        foreach (var document in models) {
            var documentLink = UriFactory.CreateDocumentUri(_databaseName, _collectionName, document["id"].ToString());
            await _client.CreateDocumentAsync(documentLink, document);
        }
    }

    public async Task DeleteAsync(string documentName, string partitionKey)
    {
        var requestOptions = new RequestOptions { PartitionKey = new PartitionKey(partitionKey) };
        var documentUri = UriFactory.CreateDocumentUri(_databaseName, _collectionName, documentName);
        await _client.DeleteDocumentAsync(documentUri, requestOptions);
    }

    public async Task DeleteMultipleAsync(string partitionKey)
    {
        var requestOptions = new RequestOptions { PartitionKey = new PartitionKey(partitionKey) };
        var query = $"SELECT * FROM c WHERE c.categoryId = '{partitionKey}'";
        var response = _client.CreateDocumentQuery<JObject>(UriFactory.CreateDocumentCollectionUri(_databaseName, _collectionName), query, new FeedOptions { EnableCrossPartitionQuery = true }).AsDocumentQuery();
        while (response.HasMoreResults)
            foreach (Document document in await response.ExecuteNextAsync())
                await _client.DeleteDocumentAsync(document.SelfLink, requestOptions);
    }
}

当我调用 SaveAsync 方法时,当它到达 await _client.CreateDocumentAsync(documentLink, document) 时出现错误。

错误是:

在 HTTP 请求中找到的 MAC 签名与计算的签名不同

由于我使用的是 Microsoft.Azure.DocumentDB,我认为它不应该抛出这个错误。

有人可以帮忙吗?

【问题讨论】:

  • 2 个问题。您是否在使用模拟器和真实服务时遇到相同的错误?您是否也可以通过 HTTPS 设置网关,看看您是否收到该错误?
  • 嘿,我的API在本地是在HTTPS上运行的,你是说它必须是真正的SSL吗?至于模拟器,我不知道有一个。有链接吗?
  • 我说的是 CosmosDB。我真正要求的只是将ConnectionMode.Direct 更改为ConnectionMode.GatewayProtocol.Tcp 更改为Protocol.Https 并告诉我这是否有效。不是解决方案,但会缩小问题范围。当然,这里是模拟器文档页面:docs.microsoft.com/en-us/azure/cosmos-db/local-emulator
  • 嗯,如果我把它改成你说的,我会得到一个新的错误:ResourceType Document is unexpected

标签: c# azure azure-cosmosdb


【解决方案1】:

原来我的保存方法创建了错误的链接。我正在使用:

var documentLink = UriFactory.CreateDocumentUri(_databaseName, _collectionName, document["id"].ToString());

应该是什么时候:

var collectionLink = UriFactory.CreateDocumentCollectionUri(_databaseName, _collectionName);

所以整个方法应该是这样的:

public async Task SaveAsync(IEnumerable<JObject> models)
{
    foreach (var document in models)
    {
        var collectionLink = UriFactory.CreateDocumentCollectionUri(_databaseName, _collectionName);
        await _client.CreateDocumentAsync(collectionLink, document);
    }
}

【讨论】:

  • 这也适用于对现有文档的更新:UpsertDocumentAsync 方法需要documentCollectionUri,而不是documentUri,即使您使用后者来首先阅读现有文档:await _client.UpsertDocumentAsync(collectionLink, document);
猜你喜欢
  • 2015-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-12
  • 1970-01-01
  • 2022-01-18
  • 2021-10-15
相关资源
最近更新 更多