コード例 #1
0
        /// <summary>
        /// Gets the universal client (Windows 10) and attempts to authenticate with OneDrive.
        /// </summary>
        /// <returns>True if the authentication was successful, otherwise false.</returns>
        public async Task <bool> InitializeAsync()
        {
            var scopes   = new[] { "wl.offline_access", "wl.signin", "office.onenote_update", "onedrive.readwrite" };
            var redirect = "urn:ietf:wg:oauth:2.0:oob";

            OneDriveClient = OneDriveClientExtensions.GetUniversalClient(scopes, redirect) as OneDriveClient;

            if (OneDriveClient == null)
            {
                return(false);
            }

            try
            {
                await OneDriveClient.AuthenticateAsync();

                Debug.WriteLine("Successfully authenticated with OneDrive");
            }
            catch (OneDriveException ex)
            {
                Debug.WriteLine("error authenticating" + ex);
                OneDriveClient.Dispose();
                return(false);
            }

            return(OneDriveClient.IsAuthenticated);
        }
コード例 #2
0
        private async void InitializeClient(ClientType clientType, RoutedEventArgs e)
        {
            if (((App)Application.Current).OneDriveClient == null)
            {
                var client = clientType == ClientType.Consumer
                    ? OneDriveClientExtensions.GetUniversalClient(this.scopes) as OneDriveClient
                    : BusinessClientExtensions.GetActiveDirectoryClient(
                    oneDriveForBusinessAppId,
                    oneDriveForBusinessReturnUrl) as OneDriveClient;

                try
                {
                    await client.AuthenticateAsync();

                    ((App)Application.Current).OneDriveClient = client;
                    ((App)Application.Current).NavigationStack.Add(new ItemModel(new Item()));
                    Frame.Navigate(typeof(MainPage), e);
                }
                catch (OneDriveException exception)
                {
                    // Swallow the auth exception but write message for debugging.
                    Debug.WriteLine(exception.Error.Message);
                    client.Dispose();
                }
            }
            else
            {
                Frame.Navigate(typeof(MainPage), e);
            }
        }
コード例 #3
0
        private async void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                oneDriveClient = OneDriveClientExtensions.GetUniversalClient(scopes);
                var accountSession = await oneDriveClient.AuthenticateAsync();

                if (accountSession != null)
                {
                    var rootItem = await oneDriveClient
                                   .Drive
                                   .Root
                                   .Request()
                                   .GetAsync();

                    var items = await oneDriveClient
                                .Drive
                                .Items[rootItem.Id]
                                .Children
                                .Request()
                                .GetAsync();
                    gridView.ItemsSource = items.CurrentPage;
                }
            }
            catch (OneDriveException oe)
            {
                txtMsg.Text = "登陆失败";
            }
        }
コード例 #4
0
        public MainPage()
        {
            InitializeComponent();

            instance = this;

            // Navigate to the first page
            Navigate(typeof(AccountsPage), this);

            if (SettingsManager.Get <bool>(Setting.UseCloudSynchronization))
            {
                PasswordVault vault = new PasswordVault();
                IReadOnlyList <PasswordCredential> credentials = vault.RetrieveAll();

                if (credentials.Any())
                {
                    credentials[0].RetrievePassword();

                    ISynchronizer synchronizer = new OneDriveSynchronizer(OneDriveClientExtensions.GetUniversalClient(new[] { "onedrive.appfolder" }));
                    IEncrypter    encrypter    = new AESEncrypter();

                    synchronizer.SetEncrypter(encrypter, credentials[0].Password);

                    AccountStorage.Instance.SetSynchronizer(synchronizer);
                }
            }

            SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
        }
コード例 #5
0
        public async Task <IOneDriveClient> LoginAsync()
        {
            try {
                if (oneDriveClient == null)
                {
                    oneDriveClient = OneDriveClientExtensions.GetUniversalClient(ServiceConstants.Scopes);
                    await oneDriveClient.AuthenticateAsync();
                }

                if (!oneDriveClient.IsAuthenticated)
                {
                    await oneDriveClient.AuthenticateAsync();
                }

                return(oneDriveClient);
            }
            catch (OneDriveException exception) {
                // Swallow authentication cancelled exceptions
                if (!exception.IsMatch(OneDriveErrorCode.AuthenticationCancelled.ToString()))
                {
                    if (exception.IsMatch(OneDriveErrorCode.AuthenticationFailure.ToString()))
                    {
                        await dialogService.ShowMessage(
                            "Authentication failed",
                            "Authentication failed");
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            return(oneDriveClient);
        }
コード例 #6
0
        public async Task <bool> AuthenticateAsync()
        {
            try
            {
                oneDriveClient = OneDriveClientExtensions.GetUniversalClient(Scopes);
                await oneDriveClient.AuthenticateAsync();
            }
            catch
            {
                System.Diagnostics.Debug.WriteLine("Fail auth. Make sure app is registered in store.");
            }

            try
            {
                // little hack to make shure the service works
                await oneDriveClient.Drive.Request().GetAsync();

                IsAuthenticated = true;
            }
            catch (Exception)
            {
                IsAuthenticated = false;
            }

            return(IsAuthenticated);
        }
コード例 #7
0
        private async void Continue_Tapped(object sender, TappedRoutedEventArgs e)
        {
            try
            {
                MainPage.ShowLoader(ResourceLoader.GetForCurrentView().GetString("SynchronizingAccountsWithCloud"));

                IOneDriveClient oneDriveClient = OneDriveClientExtensions.GetUniversalClient(new[] { "onedrive.appfolder" });
                AccountSession  session        = await oneDriveClient.AuthenticateAsync();

                IEncrypter encrypter = new AESEncrypter();

                if (session.AccountType == AccountType.MicrosoftAccount)
                {
                    synchronizer.SetEncrypter(encrypter, userKey);
                    await AccountStorage.Instance.Synchronize();

                    MainPage.HideLoader();

                    Frame.Navigate(typeof(SetupSynchronizationFinishedPage), mainPage);
                }
            }
            catch (OneDriveException ex)
            {
                MessageDialog dialog = GetOneDriveErrorMessageDialog(ex);
                await dialog.ShowAsync();
            }
        }
コード例 #8
0
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            oneDriveClient = OneDriveClientExtensions.GetUniversalClient(new[] { "onedrive.appfolder" });

            vault = new PasswordVault();

            ShowInformation();
        }
コード例 #9
0
ファイル: OnedriveManager.cs プロジェクト: ShiroYacha/Planact
 public async Task <AccountSession> Initialize()
 {
     // onedrive
     try
     {
         var scopes = new string[] { "wl.basic", "wl.signin", "onedrive.readwrite" };
         client = OneDriveClientExtensions.GetUniversalClient(scopes);
         return(await client.AuthenticateAsync());
     }
     catch (Exception)
     {
         throw;
     }
 }
コード例 #10
0
ファイル: Sync.xaml.cs プロジェクト: lukeb36/Authenticator
        private async Task DecommissionSyncWithOnedrive()
        {
            IOneDriveClient onedriveClient =
                OneDriveClientExtensions.GetUniversalClient(new[] { "onedrive.appfolder" });

            IReadOnlyList <PasswordCredential> credentials = vault.RetrieveAll();

            foreach (PasswordCredential credential in credentials)
            {
                vault.Remove(credential);
            }
            await onedriveClient.SignOutAsync();

            SettingsManager.Save(Setting.UseCloudSynchronization, false);
        }
コード例 #11
0
        private async void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (((App)Application.Current).OneDriveClient == null)
            {
                ((App)Application.Current).OneDriveClient = OneDriveClientExtensions.GetUniversalClient(
                    this.scopes);

                await((App)Application.Current).OneDriveClient.AuthenticateAsync();
            }

            if (this.itemsController == null)
            {
                this.itemsController = new ItemsController(((App)Application.Current).OneDriveClient);
            }

            var last = ((App)Application.Current).NavigationStack.Last();

            ((App)Application.Current).Items = await this.itemsController.GetImagesAndFolders(last.Id);

            this.DataContext = ((App)Application.Current).Items;
            wait.IsActive    = false;
        }
コード例 #12
0
        /// <summary>
        /// Tests logging into MobileService with Microsoft Account token (via OneDrive SDK).
        /// App needs to be assosciated with a WindowsStoreApp
        /// </summary>
        private async Task TestClientDirectedMicrosoftAccountLogin()
        {
            try
            {
                Task <AccountSession> sessionTask = null;

                // Force AuthenticateAsync() to run on the UI thread.
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    IOneDriveClient oneDriveClient = OneDriveClientExtensions.GetUniversalClient(new string[] { "wl.signin" });
                    sessionTask = oneDriveClient.AuthenticateAsync();
                });

                AccountSession session = await sessionTask;

                if (session != null && session.AccessToken != null)
                {
                    JObject accessToken = new JObject();
                    accessToken["access_token"] = session.AccessToken;

                    MobileServiceUser user = await this.GetClient().LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount, accessToken);

                    Log(string.Format("Log in with Microsoft Account OneDrive SDK succeeded - userId {0}", user.UserId));
                }
                else
                {
                    Assert.Fail("Log in with Microsoft Account OneDrive SDK failed");
                }
            }
            catch (Exception exception)
            {
                Log(string.Format("ExceptionType: {0} Message: {1} StackTrace: {2}",
                                  exception.GetType().ToString(),
                                  exception.Message,
                                  exception.StackTrace));
                Assert.Fail("Log in with Microsoft Account OneDrive SDK failed");
            }
        }