How to extend the Expiry of a Document DB REST API Resource Token

天涯浪子 提交于 2019-12-12 01:20:01

问题


I am monitoring the entries in a document database collection by means of REST API from a front end application.

The RESP API queries for a list of documents in the collection based on certain filter criteria.

The token for authenticating the REST API call is generated by means of the .NET SDK.

Here's the code snippet used for the token generation :

 string GenerateAuthToken(string verb, string resourceId, string resourceType, string key, string keyType, string tokenVersion)
    {
        var hmacSha256 = new System.Security.Cryptography.HMACSHA256 { Key = Convert.FromBase64String(key) };

        string verbInput = verb ?? "";
        string resourceIdInput = resourceId ?? "";
        string resourceTypeInput = resourceType ?? "";

        string dateString = DateTime.UtcNow.ToString("r").ToLower();

        string payLoad = string.Format(System.Globalization.CultureInfo.InvariantCulture,
            "{0}\n{1}\n{2}\n{3}\n{4}\n",
                verb.ToLowerInvariant(),
                resourceType.ToLowerInvariant(),
                resourceId,
                dateString,
                ""
        );

        byte[] hashPayLoad = hmacSha256.ComputeHash(System.Text.Encoding.UTF8.GetBytes(payLoad));
        string signature = Convert.ToBase64String(hashPayLoad);

        return System.Web.HttpUtility.UrlEncode(String.Format(System.Globalization.CultureInfo.InvariantCulture, "type={0}&ver={1}&sig={2}",
            keyType,
            tokenVersion,
            signature));
    }

As per the API documentation:

Resource tokens must be generated by an intermediate server. The server serves as the master-key guardian and generates time-constrained tokens for untrusted clients, such as web browsers.

What is the default expiry time for this token? Is there a way to extend the expiry of the token?


回答1:


By default, the validity period of a resource token is 1 hour. The validity period can be overridden to up to 5 hours.

If you’re using REST, it must be set in the “x-ms-documentdb-expiry-seconds” header when you create/replace/read Permission.

For more information on how to create one, please refer to https://docs.microsoft.com/en-us/rest/api/documentdb/permissions



来源:https://stackoverflow.com/questions/41761552/how-to-extend-the-expiry-of-a-document-db-rest-api-resource-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!