public bool RetrieveCredentialCache(CredentialCache credentialCache) { if (data == null) { return(false); } credentialCache.InitializeCacheFromBlob(data); return(true); }
/// <summary> /// Gets the client. /// </summary> /// <returns></returns> private static async Task <IOneDriveClient> GetClient() { CredentialCache credentialCache = new CredentialCache(); string credentials = OneDriveCloudStorageServiceSettings.Default.Credentials; if (!String.IsNullOrWhiteSpace(credentials)) { credentialCache.InitializeCacheFromBlob(Encoding.Default.GetBytes(credentials)); } IServiceInfoProvider serviceInfoProvider = new ServiceInfoProvider(new FormsWebAuthenticationUi()) { UserSignInName = OneDriveCloudStorageServiceSettings.Default.UserId }; return(await OneDriveClient.GetAuthenticatedMicrosoftAccountClient( Util.Decrypt(OneDriveCloudStorageServiceSettings.Default.AppKey, CultureConstants.InvariantCulture.NativeName), RedirectUri, s_scopes, serviceInfoProvider, credentialCache).ConfigureAwait(false)); }
public async Task ResotreAuthenticateUser() { byte[] cacheBlod = SettingsWorker.Current.GetCahceBlod(); if (cacheBlod == null) { return; } _credentialCache.InitializeCacheFromBlob(cacheBlod); _msaAuthenticationProvider = new MsaAuthenticationProvider(APP_ID, ReturnURL, _scopes, _credentialCache); try { await _msaAuthenticationProvider.RestoreMostRecentFromCacheAsync(); if (_msaAuthenticationProvider.IsAuthenticated) { _oneDriveClient = new OneDriveClient(BaseURL, _msaAuthenticationProvider); IsAuthenticated = true; } } catch { } }
private void Initialize() { if (Client != null) { return; } // Load credential cache CredentialCache credentialCache = new CredentialCache(); if (!string.IsNullOrEmpty(Settings.Default.Credentials)) { credentialCache.InitializeCacheFromBlob(Convert.FromBase64String(Settings.Default.Credentials)); } // Authenticate with OneDrive Func <CredentialCache, AccountSession> authenticate = cc => { Client = OneDriveClient.GetMicrosoftAccountClient(applicationId, applicationReturnUrl, applicationScopes, applicationSecret, cc, null, new OneDriveInfoProvider()); Task <AccountSession> oneDriveSessionTask = Client.AuthenticateAsync(); oneDriveSessionTask.Wait(); return(oneDriveSessionTask.Result); }; try { Session = authenticate(credentialCache); } catch { credentialCache = new CredentialCache(); Session = authenticate(credentialCache); } // Save credentials if (Session == null) { Settings.Default.Credentials = null; Settings.Default.Save(); } else if (credentialCache.HasStateChanged) { Settings.Default.Credentials = Convert.ToBase64String(credentialCache.GetCacheBlob()); Settings.Default.Save(); } // Find specified root folder if (!Path.StartsWith("/") || !IsPathValid(Path)) { throw new Exception("The specified path is not valid"); } Task <Item> task; if (Path.Length == 1) { task = Client.Drive.Root.Request().GetAsync(); } else { task = Client.Drive.Root.ItemWithPath(Path.Trim('/')).Request().GetAsync(); } task.Wait(); root = task.Result; }