Leandro
Leandro

Reputation: 13

DocumentDB + Xamarin - The authorization token is not valid

I'm trying to resolve a critical bug in my app made with Xamarin Forms + DocumentDB. I'm using the package Microsoft.Azure.DocumentDB.Core to store data. Our Azure account is hosted in Brazil South (GMT-3), and the apps works normally. But we notice that in districts with the locale time GMT-4, the app crash with the exception above.

The authorization token is not valid at the current time. Please 
create another token and retry (token start time: Mon, 27 Mar 2017
00:07:41 GMT, token expiry time: Mon, 27 Mar 2017 00:22:41 GMT, 
current server time: Mon, 27 Mar 2017 01:08:24 GMT).
ActivityId: 81487924-68ee-4329-bb61-02f88ea7b6ec

If I delayed my device one hour to get GMT-4, and run the app I can see the exception.

    //---------------------------------------------static Repository initialize
    DocumentClient client;
    const string collectionId = "user";
    Uri uri = UriFactory.CreateDocumentCollectionUri(Constants.DB_ID, collectionId);

    //---------------------------------------------static constructor
    UserDataService()
    {
        client = new DocumentClient(
                new Uri(Constants.DB_ACCOUNT_URL),
                        Constants.DB_ACCOUNT_KEY);
    }

    async public Task<User> GetUserByCPF(string cpf)
    {
        var fedOpt = new FeedOptions { MaxItemCount = -1 };
        var query = client.CreateDocumentQuery<User>(uri, fedOpt)
                          .Where(x => x.Id == _id)
                          .AsDocumentQuery();

        var lst = new List<User>();
        while (query.HasMoreResults)
        {
            lst.AddRange(await query.ExecuteNextAsync<User>());
        }

        return lst.FirstOrDefault();
    }

The exceptions rises in query.ExecuteNextAsync;

The error message says to create another token, but I'm using the Master Key. Someone know how create a token to Master Key or increase the expiration date?

Upvotes: 1

Views: 701

Answers (1)

Tom Sun
Tom Sun

Reputation: 24549

Base on my experience, the date portion of the string is the UTC date to generate token by SDK. And I can't repro the issue with differen zone on my side.The only way I get the exception is that local device system is incorrect.

enter image description here

So I assume that the exception is caused by the device using incorrect time.

Upvotes: 1

Related Questions