Esempio n. 1
0
        /// <summary>
        /// Validates if the session has authenticated already and if so, ensures the AccessToken coming forward from the authentication is still valid and assigns it to the HttpClient in this session
        /// </summary>
        private async Task EnsureAccessToken()
        {
            // Check if we have an authenticated session
            if (AuthenticatedSession == null || !AuthenticatedSession.Expires.HasValue)
            {
                // Session is not yet authenticated, nothing we can do at this point
                return;
            }

            // We have an authenticated session, check if its still valid
            if (AuthenticatedSession.Expires.Value < DateTime.Now)
            {
                // Access token is no longer valid, request a new one
                if (!string.IsNullOrEmpty(AuthenticatedSession.RefreshToken))
                {
                    // We have a refresh token, use that to get a new access token
                    AuthenticatedSession = await GetRefreshedSession();
                }
                else
                {
                    // We don't have a refresh token, just get a new access token
                    AuthenticatedSession = await GetNewSession();
                }
            }

            // Set the access token on the HttpClient
            _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", AuthenticatedSession.AccessToken);
        }
Esempio n. 2
0
 /// <summary>
 /// Authenticates this session with the Tado API
 /// </summary>
 public async Task Authenticate()
 {
     try
     {
         // Request the OAuth token
         AuthenticatedSession = await GetNewSession();
     }
     catch (Exception ex)
     {
         throw new Exceptions.SessionAuthenticationFailedException(ex);
     }
 }