/// <summary> /// main window load method that checks the valid session /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void mainWindow_Loaded(object sender, RoutedEventArgs e) { try { session = MyOAuth.getSession(); Config = new BoxConfig(BoxManagerConst.client_id, BoxManagerConst.client_secret, new Uri("https://" + BoxManagerConst.redirecturi)); Client = new BoxClient(Config, session); Client.Auth.SessionAuthenticated += Auth_SessionAuthenticated; try { Box.V2.Models.BoxUser u = await Client.UsersManager.GetCurrentUserInformationAsync(); if (u != null) { webBrowser.Visibility = Visibility.Hidden; //BoxTokenGenerator.ActiveForm.Text += " - " + u.Login; loggedin_user = u.Login; //boxMenu.Visible = true; main.Visibility = Visibility.Visible; //txtToken.Text = session.AccessToken + "#" + session.RefreshToken; txtAccessToken.Text = session.AccessToken; txtRefreshToken.Text = session.RefreshToken; lblLoggedInUser.Text = loggedin_user; mainWindow.Height = formHeight; } } catch (BoxException) { webBrowser.Visibility = Visibility.Visible; session = null; webBrowser.Source = new Uri(this.request); mainWindow.Height = 600; } //catch (Exception ex) //{ // if (ex.Message.Contains("BoxSessionInvalidated")) // { // webBrowser.Visibility = Visibility.Visible; // main.Visibility = Visibility.Hidden; // session = null; // webBrowser.Source = new Uri(this.request); // mainWindow.Height = 600; // } //} } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK); } }
/// <summary> /// Web Browser redirection in case of first login/logout or corrupted tokens /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void webBrowser_Navigated(object sender, NavigationEventArgs e) { if (e.Uri.Host == BoxManagerConst.redirecturi) { // grab access_token and oauth_verifier IDictionary <string, string> keyDictionary = new Dictionary <string, string>(); var qSplit = e.Uri.Query.Split('?'); foreach (var kvp in qSplit[qSplit.Length - 1].Split('&')) { var kvpSplit = kvp.Split('='); if (kvpSplit.Length == 2) { keyDictionary.Add(kvpSplit[0], kvpSplit[1]); } } AuthCode = keyDictionary["code"]; webBrowser.Visibility = Visibility.Hidden; try { session = await Client.Auth.AuthenticateAsync(AuthCode); TokenConfig.writeTokens(session.AccessToken, session.RefreshToken); Box.V2.Models.BoxUser u = await Client.UsersManager.GetCurrentUserInformationAsync(); loggedin_user = u.Login; lblLoggedInUser.Text = loggedin_user; main.Visibility = Visibility.Visible; txtAccessToken.Text = session.AccessToken; txtRefreshToken.Text = session.RefreshToken; mainWindow.Height = formHeight; } catch (BoxException ex) { MessageBox.Show(ex.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Error); } catch (Exception ex) { MessageBox.Show(ex.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Error); } } }