/// <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); }
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); } }
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 = "登陆失败"; } }
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; }
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); }
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); }
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(); } }
private void Page_Loaded(object sender, RoutedEventArgs e) { oneDriveClient = OneDriveClientExtensions.GetUniversalClient(new[] { "onedrive.appfolder" }); vault = new PasswordVault(); ShowInformation(); }
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; } }
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); }
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; }
/// <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"); } }