private async Task InitOneDrive()
        {
            string[] scopes = { "onedrive.readonly", "wl.signin" };
            try
            {
                // get provider
                authenticationProvider = new OnlineIdAuthenticationProvider(scopes);
                await authenticationProvider.RestoreMostRecentFromCacheOrAuthenticateUserAsync();

                // init client
                oneDriveClient = new OneDriveClient(BaseUrl, authenticationProvider);
            }
            catch (Exception e)
            {
                Analytics.TrackEvent("Exception when initializing OneDrive", new Dictionary <string, string>
                {
                    { "Error", e.Message },
                    { "InnerError", e.InnerException != null ? e.InnerException.Message : "" },
                    { "Where", "MainPage.xaml:InitOneDrive" }
                });
                authenticationProvider = null;
                oneDriveClient         = null;
                throw;
            }
        }
Example #2
0
        private async Task loggin()
        {
            msaProvider = new OnlineIdAuthenticationProvider(scopes);
            await msaProvider.RestoreMostRecentFromCacheOrAuthenticateUserAsync();

            this.client = new OneDriveClient(msaProvider);
        }
        private async void InitializeClient(ClientType clientType, RoutedEventArgs e)
        {
            var app = (App)Application.Current;

            if (app.OneDriveClient == null)
            {
                Task authTask;

                if (clientType == ClientType.Business)
                {
                    var adalAuthProvider = new AdalAuthenticationProvider(
                        this.oneDriveForBusinessClientId,
                        this.oneDriveForBusinessReturnUrl);
                    authTask           = adalAuthProvider.AuthenticateUserAsync(this.oneDriveForBusinessBaseUrl);
                    app.OneDriveClient = new OneDriveClient(this.oneDriveForBusinessBaseUrl + "/_api/v2.0", adalAuthProvider);
                    app.AuthProvider   = adalAuthProvider;
                }
                else if (clientType == ClientType.ConsumerUwp)
                {
                    var onlineIdAuthProvider = new OnlineIdAuthenticationProvider(
                        this.scopes);
                    authTask           = onlineIdAuthProvider.RestoreMostRecentFromCacheOrAuthenticateUserAsync();
                    app.OneDriveClient = new OneDriveClient(this.oneDriveConsumerBaseUrl, onlineIdAuthProvider);
                    app.AuthProvider   = onlineIdAuthProvider;
                }
                else
                {
                    var msaAuthProvider = new MsaAuthenticationProvider(
                        this.oneDriveConsumerClientId,
                        this.oneDriveConsumerReturnUrl,
                        this.scopes,
                        new CredentialVault(this.oneDriveConsumerClientId));
                    authTask           = msaAuthProvider.RestoreMostRecentFromCacheOrAuthenticateUserAsync();
                    app.OneDriveClient = new OneDriveClient(this.oneDriveConsumerBaseUrl, msaAuthProvider);
                    app.AuthProvider   = msaAuthProvider;
                }

                try
                {
                    await authTask;
                    app.NavigationStack.Add(new ItemModel(new Item()));
                    this.Frame.Navigate(typeof(MainPage), e);
                }
                catch (ServiceException exception)
                {
                    // Swallow the auth exception but write message for debugging.
                    Debug.WriteLine(exception.Error.Message);
                }
            }
            else
            {
                this.Frame.Navigate(typeof(MainPage), e);
            }
        }
Example #4
0
        /// <summary>
        ///     Perform an async Login Request
        /// </summary>
        public async Task <IOneDriveClient> LoginAsync()
        {
            try
            {
                var msaAuthenticationProvider = new OnlineIdAuthenticationProvider(ServiceConstants.Scopes);

                await msaAuthenticationProvider.RestoreMostRecentFromCacheOrAuthenticateUserAsync();

                return(new OneDriveClient(ServiceConstants.BASE_URL, msaAuthenticationProvider));
            }
            catch (ServiceException serviceException)
            {
                Debug.WriteLine(serviceException);
                throw new BackupException("Authentication Failed with Graph.ServiceException", serviceException);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                throw new BackupException("Authentication Failed", ex);
            }
        }