Exemplo n.º 1
0
 /// <summary>
 /// Converts <see cref="ApplicationDataCompositeValue"/> instance to <see cref="MSHealthToken"/>.
 /// </summary>
 /// <param name="composite"><see cref="ApplicationDataCompositeValue"/> instance.</param>
 /// <returns><see cref="MSHealthToken"/> instancel.</returns>
 public static MSHealthToken ToMSHealthToken(this ApplicationDataCompositeValue composite)
 {
     MSHealthToken loToken = null;
     if (composite != null)
     {
         loToken = new MSHealthToken();
         if (composite["creationTime"] != null)
             loToken.CreationTime = new DateTime((long)composite["creationTime"]);
         if (composite["expiresIn"] != null)
             loToken.ExpiresIn = (long)composite["expiresIn"];
         if (composite["accessToken"] != null)
             loToken.AccessToken = (string)composite["accessToken"];
         if (composite["refreshToken"] != null)
             loToken.RefreshToken = (string)composite["refreshToken"];
     }
     return loToken;
 }
Exemplo n.º 2
0
 /// <summary>
 /// Verifies <paramref name="token"/>instance validity (<see cref="MSHealthToken.ExpirationTime"/>),
 /// replaces current <see cref="MSHealthClient.Token"/> and if <paramref name="refreshIfInvalid"/>
 /// is <see langword="true"/>, calls <see cref="MSHealthClient.RefreshToken"/>.
 /// </summary>
 /// <param name="token">Instance of <see cref="MSHealthToken"/> to validate.</param>
 /// <param name="refreshIfInvalid">Flag to enforce Token refresh if is not valid.</param>
 /// <returns><see langword="true"/> if specified token is valid or has been refresh successfull, otherwise, <see langword="false"/>.</returns>
 /// <remarks>
 /// It only wworks if <see cref="MSHealthToken.RefreshToken"/> is available, to obtain it,
 /// it's necessary to set use <see cref="MSHealthScope.OfflineAccess"/>.
 /// </remarks>
 public async Task<bool> ValidateToken(MSHealthToken token, bool refreshIfInvalid = true)
 {
     bool lbValid = false;
     // Check input token
     if (token != null)
     {
         // Check instance token
         if (Token != null)
         {
             // Compare AccessToken for input vs instance
             if (!string.IsNullOrEmpty(token.AccessToken) &&
                 !token.AccessToken.Equals(Token.AccessToken, StringComparison.OrdinalIgnoreCase))
             {
                 // Different AccessToken, so, asign input to instance
                 Token = token;
             }
         }
         else
         {
             // No instance token, so, asign input
             Token = token;
         }
         // Check Token's ExpirationTime
         if (Token.ExpirationTime == null ||
             Token.ExpirationTime.CompareTo(DateTime.Now) <= 0)
         {
             // Already Expired, so, refresh Token
             if (refreshIfInvalid)
                 lbValid = await RefreshToken();
         }
         else
         {
             lbValid = true;
         }
     }
     return lbValid;
 }