/// <summary>
        /// Log in an account and add it to the list of connected accounts
        /// </summary>
        /// <param name="tenantId">Active Directory Tenant Id</param>
        /// <param name="clientId">Client Id of the Azure AD App</param>
        /// <param name="secretKey">Secret Key of the Azure AD App</param>
        /// <returns>The created Account entity</returns>
        public async Task <Account> LoginAsync(string tenantId, string clientId, string secretKey)
        {
            if (string.IsNullOrEmpty(tenantId) ||
                string.IsNullOrEmpty(clientId) ||
                string.IsNullOrEmpty(secretKey))
            {
                return(null);
            }

            try
            {
                var token = await _oAuthClient.GetTokenAsync(tenantId, clientId, secretKey);

                if (token == null)
                {
                    return(null);
                }

                var account = new Account()
                {
                    Name        = tenantId,
                    TenantId    = tenantId,
                    ClientId    = clientId,
                    SecretKey   = secretKey,
                    AccessToken = token.AccessToken,
                    AccessTokenExpirationDate = DateTime.Now.Add(token.ExpiresIn)
                };

                var isAdded = await _accountRepository.AddAsync(account);

                return(isAdded ? account : null);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
예제 #2
0
        public async Task <Token> GetTokenAsync(OAuthOptions configuration, CancellationToken cancellationToken)
        {
            Token token = _tokenCache.GetToken(configuration);

            if (token != null && !token.IsAccessTokenExpired)
            {
                return(token);
            }

            // TODO: use refresh token if available
            token = await _client.GetTokenAsync(configuration, cancellationToken);

            _tokenCache.SaveToken(configuration, token);

            return(token);
        }
예제 #3
0
 private async Task SetBearerToken()
 {
     _httpClient.SetBearerToken(await _oAuthClient.GetTokenAsync());
 }