Ejemplo n.º 1
0
        /// <summary>
        /// This method can only be used with <see cref="Authority.Organizations"/>
        /// Only used in test.
        /// </summary>
        public async Task <AuthenticationResult> LoginWithUsernamePassword(string username = null, string password = null)
        {
            this.username = username ?? this.username;
            this.password = password ?? this.password;
            if (this.username == null || this.password == null)
            {
                return(null);
            }
            BeforeTaskExecuted?.Invoke(this, null);
            using var cts = new CancellationTokenSource(Timeouts.Silent);
            var secureString = new SecureString();

            foreach (var c in this.password ?? "")
            {
                secureString.AppendChar(c);
            }
            var result = await msalClient
                         .AcquireTokenByUsernamePassword(scopes, this.username, secureString)
                         .ExecuteAsync(cts.Token).ConfigureAwait(false);

            var account = result?.Account;

            RegisterUser(account);

            TaskExecuted?.Invoke(this, null);
            return(result);
        }
Ejemplo n.º 2
0
        public async IAsyncEnumerable <DriveItem> GetChildrenAsync(IAccount account, string parentId)
        {
            BeforeTaskExecuted?.Invoke(this, null);
            var userId = GetUserId(account);

            using var cts = new CancellationTokenSource(Timeouts.Silent);
            var request = graphClient.Users[userId].Drive.Items[parentId].Children.Request();

            do
            {
                IDriveItemChildrenCollectionPage page;
                try {
                    page = await request.GetAsync(cts.Token).ConfigureAwait(false);
                } catch (TaskCanceledException ex) {
                    logger?.LogError(ex, "Use canncel the task.");
                    TaskExecuted?.Invoke(this, null);
                    yield break;
                } catch (ServiceException ex) {
                    logger?.LogError(ex, "onedrive server error.");
                    TaskExecuted?.Invoke(this, null);
                    yield break;
                }
                foreach (var file in page)
                {
                    yield return(file);
                }
                request = page?.NextPageRequest;
            } while (request != null);
            TaskExecuted?.Invoke(this, null);
        }
Ejemplo n.º 3
0
        public async Task <Stream> GetFileContentAsync(IAccount account, string fileId)
        {
            BeforeTaskExecuted?.Invoke(this, null);
            var userId = GetUserId(account);
            var cts    = new CancellationTokenSource(Timeouts.Silent);

            try {
                return(await graphClient.Users[userId].Drive.Items[fileId].Content.Request().GetAsync(cts.Token).ConfigureAwait(false));
            } catch (ServiceException ex) {
                logger?.LogError(ex, "onedrive server error");
                return(null);
            } finally {
                TaskExecuted?.Invoke(this, null);
            }
        }
Ejemplo n.º 4
0
        public async Task <bool> LogoutAsync(IAccount account)
        {
            BeforeTaskExecuted?.Invoke(this, null);
            try {
                // TODO: this method just clears the cache without truely logout the user!!
                await msalClient.RemoveAsync(account).ConfigureAwait(false);

                accountList.Remove(account);
                return(true);
            } catch (Exception ex) {
                logger?.LogError(ex, "");
                return(false);
            } finally {
                TaskExecuted?.Invoke(this, null);
            }
        }
Ejemplo n.º 5
0
        public async Task <User> GetUserAsync(IAccount account)
        {
            BeforeTaskExecuted?.Invoke(this, null);
            var userId = GetUserId(account);

            using var cts = new CancellationTokenSource(Timeouts.Silent);
            try {
                var user = await graphClient.Users[userId].Request().GetAsync(cts.Token).ConfigureAwait(false);
                return(user);
            } catch (ServiceException ex) {
                logger?.LogError(ex, "onedrive server error");
                return(null);
            } finally {
                TaskExecuted?.Invoke(this, null);
            }
        }
Ejemplo n.º 6
0
        public async Task <AuthenticationResult> LoginInteractively(CancellationToken token)
        {
            BeforeTaskExecuted?.Invoke(this, null);
            var requestBuilder = msalClient.AcquireTokenInteractive(scopes);

            try {
                var result = await requestBuilder
                             .ExecuteAsync(token)
                             .ConfigureAwait(false);

                RegisterUser(result?.Account);
                return(result);
            } catch (MsalClientException) {
                logger?.LogInformation("User cancelled.");
            } catch (MsalException ex) {
                logger?.LogWarning(ex, "Masl exception.");
            } catch (InvalidOperationException ex) {
                logger?.LogError(ex, "");
            }
            TaskExecuted?.Invoke(this, null);
            return(null);
        }
Ejemplo n.º 7
0
        public async IAsyncEnumerable <AuthenticationResult> LoginAllUserSilently()
        {
            BeforeTaskExecuted?.Invoke(this, null);
            var accounts = await msalClient.GetAccountsAsync().ConfigureAwait(false);

            foreach (var account in accounts)
            {
                AuthenticationResult result;
                try {
                    using var cts = new CancellationTokenSource(Timeouts.Silent);
                    result        = await msalClient.AcquireTokenSilent(scopes, account)
                                    .ExecuteAsync(cts.Token)
                                    .ConfigureAwait(false);

                    RegisterUser(result?.Account);
                } catch (Exception ex) {
                    logger?.LogError(ex, "onedrive server error");
                    continue;
                }
                yield return(result);
            }
            TaskExecuted?.Invoke(this, null);
        }