private async Task <Notification> CheckForValidResourceTokenAsync() { var result = Notification.Success(); if (TokenExpiry.ToUniversalTime() < DateTime.UtcNow.Add(TimeSpan.FromMinutes(5))) { Debug.WriteLine("The Cosmos Resource Token is about to expire. Let's get a new one...."); await FetchResourceTokenAsync(); } return(result); }
/// <summary> /// Construct a new token by parsing userCredentialJson. /// </summary> public ActiveUserToken(string userCredentialJson, string user) { User = user; JToken parsedCredentialJson = JObject.Parse(userCredentialJson); JToken accessTokenJson = parsedCredentialJson.SelectToken("access_token"); if (accessTokenJson == null || accessTokenJson.Type != JTokenType.String) { throw new InvalidDataException("Credential JSON should contain access token key."); } AccessToken = accessTokenJson.Value <string>(); JToken tokenExpiryJson = parsedCredentialJson.SelectToken("token_expiry"); // Service account credentials do not expire. if (tokenExpiryJson == null || tokenExpiryJson.Type == JTokenType.Null) { ExpiredTime = DateTime.MaxValue; } else { TokenExpiry tokenExpiry = tokenExpiryJson.ToObject <TokenExpiry>(); if (tokenExpiry == null) { throw new InvalidDataException("Credential JSON contains an invalid token_expiry."); } ExpiredTime = new DateTime( tokenExpiry.Year, tokenExpiry.Month, tokenExpiry.Day, tokenExpiry.Hour, tokenExpiry.Minute, tokenExpiry.Second, tokenExpiry.MicroSecond / 1000, DateTimeKind.Utc); } }