private void OnWebBrowserNavigated(object sender, System.Windows.Navigation.NavigationEventArgs e) { var dict = HttpUtility.ParseQueryString(e.Uri.Query); string code = dict.Get("code"); if (code != null) { try { var newAccount = new OneDriveAccount(OneDriveClient.AcquireClientByCode(code)); var userId = newAccount.Client.UserData.Id; var existingConnectedUser = AppSettings.Instance.Accounts.FirstOrDefault(a => a.Client.UserData.Id == userId); if (existingConnectedUser == null) { AppSettings.Instance.Accounts.Add(newAccount); } else { existingConnectedUser.Client.CredentialData = newAccount.Client.CredentialData; } BrowserHolder.Visibility = Visibility.Collapsed; ClearIECache(); } catch (System.Exception ex) { ErrorMessageBox.ShowDialog(); } } if (dict.Get("error") != null) { BrowserHolder.Visibility = Visibility.Collapsed; ErrorMessageBox.ShowDialog(); } }
//public static IPublicClientApplication PublicClientApp; private void OnConnectButtonClick(object sender, RoutedEventArgs e) { BrowserHolder.Visibility = Visibility.Visible; var authStr = OneDriveClient.GetAuthorizationRequestUrl(); Browser.Navigate(authStr); }
private Task RenewAccessToken() { if (CredentialData.IsValid()) { return(Task.CompletedTask); } if (currentRenewer != null) { currentRenewer.Wait(); return(Task.CompletedTask); } else { currentRenewer = Task.Run(() => { string userResult = null; using (var wc = new WebClient()) { try { if (CredentialData.RefreshToken == null) { throw new HttpException((int)HttpStatusCode.Unauthorized, " Credential data is empty"); } string body = OneDriveClient.MakeRefreshTokenRequestBody(CredentialData.RefreshToken); wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; userResult = wc.UploadString("https://login.microsoftonline.com/common/oauth2/v2.0/token", body); } catch (Exception ex) { logger.Error(ex, "Renew Access token error"); //NeedRelogin?.Invoke(this); //return; } } try { CredentialData = JsonConvert.DeserializeObject <Credentials>(userResult); } catch (System.Exception ex) { logger.Error(ex, "Parse credential data exception"); if (ex is WebException) { throw ex; } } }).ContinueWith((t) => currentRenewer = null); } return(currentRenewer); }
public static OneDriveClient AcquireClientByCode(string code) { string dataResult; using (var wc = new WebClient()) { string body = OneDriveClient.MakeAcquireTokenByAuthorizationCodeContent(code); wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; dataResult = wc.UploadString("https://login.microsoftonline.com/common/oauth2/v2.0/token", body); } try { var result = new OneDriveClient(JsonConvert.DeserializeObject <Credentials>(dataResult)); return(result); } catch (System.Exception ex) { logger.Error(ex, "Parse credentioal data error"); return(null); } }
public OneDriveAccount(OneDriveClient client) { Client = client; BindingOperations.EnableCollectionSynchronization(CurrentWorkers, currentWorkersLock); }