Ejemplo n.º 1
0
        public async Task <bool> AuthenticateNew(CancellationToken cs)
        {
            msaAuthenticationProvider = new MsaAuthenticationProvider(MicrosoftSecret.ClientId, MicrosoftSecret.ClientSecret, "http://localhost:45674/authredirect", Scopes, null, new CredentialsVault(this, null));
            await msaAuthenticationProvider.AuthenticateUserAsync().ConfigureAwait(false);

            oneDriveClient = new OneDriveClient("https://api.onedrive.com/v1.0", msaAuthenticationProvider);

            return(msaAuthenticationProvider.IsAuthenticated);
        }
Ejemplo n.º 2
0
        internal async void Authenticate()
        {
            MsaAuthenticationProvider msaAuthProvider = new MsaAuthenticationProvider(clientID, "https://login.live.com/oauth20_desktop.srf", scopes);

            await msaAuthProvider.AuthenticateUserAsync();

            OneDriveClient oneDriveClient = new OneDriveClient("https://api.onedrive.com/v1.0", msaAuthProvider);

            OneDriveClient = oneDriveClient;
        }
Ejemplo n.º 3
0
        public async void Starter(string accessToken)
        {
            string[] scopes = new string[] { "onedrive.readonly", "wl.signin" };
            var      msaAuthenticationProvider = new MsaAuthenticationProvider(accessToken,
                                                                               "https://login.live.com/oauth20_desktop.srf",
                                                                               scopes);
            await msaAuthenticationProvider.AuthenticateUserAsync();

            oneDriveClient = new OneDriveClient(msaAuthenticationProvider);

            Getroot();
        }
Ejemplo n.º 4
0
        public async Task Login()
        {
            try
            {
                _msaAuthenticationProvider = new MsaAuthenticationProvider(APP_ID, ReturnURL, _scopes);

                await _msaAuthenticationProvider.AuthenticateUserAsync();

                _credentialCache = _msaAuthenticationProvider.CredentialCache;

                _oneDriveClient = new OneDriveClient(BaseURL, _msaAuthenticationProvider);

                IsAuthenticated = _msaAuthenticationProvider.IsAuthenticated;
            }
            catch { }
        }
Ejemplo n.º 5
0
        private async Task Authenticate(OneDriveAccount acc)
        {
            string[] scopes = { "onedrive.readwrite", "wl.signin" };

            var msaAuthProvider = new MsaAuthenticationProvider(
                AppInfo.OneDriveClientId,
                /*"https://login.live.com/oauth20_desktop.srf"*/ null,
                scopes);

            await msaAuthProvider.AuthenticateUserAsync();

            if (!msaAuthProvider.IsAuthenticated)
            {
                throw new Exception("Failed to authenticate One Drive client with credentials provided.");
            }
            client = new OneDriveClient("https://api.onedrive.com/v1.0", msaAuthProvider);
        }
Ejemplo n.º 6
0
        private async Task <HttpResponseMessage> MakeOneNoteRequest(string url, HttpMethod method, IHttpContent content = null, bool validatesPageId = true)
        {
            if (validatesPageId)
            {
                // Making the assumption that the ID this method got passed is the same as the one in the cache.
                // If this turns out to be an invalid assumption, we'll have to do some string parsing, or pass
                // in the ID separately or something.
                string oldId           = CachedPageId;
                var    validatedPageId = await ValidatePageId(CachedPageId);

                if (!validatedPageId.HasValue)
                {
                    return(new HttpResponseMessage(HttpStatusCode.BadRequest));
                }
                string newId = validatedPageId.ValueOrFailure();
                if (newId != oldId)
                {
                    // Hopefully we never have a scenario where the oldId isn't actually in the request URL...
                    // If we DO, that's two reasons to parse out the ID
                    url = url.Replace(oldId, newId);
                }
            }

            var message = new HttpRequestMessage(method, new Uri(url));

            message.Content = content;

            var response = await _httpClient.SendRequestAsync(message);

            // For now, if we get an auth failure, only try to re-auth and make a new request once.
            // If this starts happening a lot, maybe we can add exponential back-off or something.
            if (response?.StatusCode == HttpStatusCode.Unauthorized)
            {
                await _msaAuthProvider.AuthenticateUserAsync();

                response = await _httpClient.SendRequestAsync(message);

                return(response);
            }
            else
            {
                return(response);
            }
        }
        public async Task <Item> GetItemsFromSelectionAsync(IOneDriveClient oneDriveClient = null)
        {
            const string msa_client_id = "0000000044128B55";
            var          offers        = new string[] { "onedrive.readwrite", "wl.signin" };

            if (oneDriveClient == null)
            {
                var authProvider = new MsaAuthenticationProvider(msa_client_id, FormBrowser.MsaReturnUrl, offers);
                oneDriveClient = new OneDriveClient(authProvider);

                await authProvider.AuthenticateUserAsync();
            }

            var parts    = this.SelectionId.Split('.');
            var bundleID = parts[2];

            return
                (await
                 oneDriveClient.Drive.Items[bundleID].Request().Expand("thumbnails,children(expand=thumbnails)").GetAsync());
        }
Ejemplo n.º 8
0
        public async Task Authenticate()
        {
            try
            {
                if (authenticator == null)
                {
                    InitAuthenticator();
                }
                await authenticator.AuthenticateUserAsync();

                client = new OneDriveClient("https://api.onedrive.com/v1.0", authenticator);
            }
            catch (Microsoft.Graph.ServiceException ex)
            {
                if (ex.Error.Code != "authenticationFailure" && ex.Error.Code != "authenticationCanceled")
                {
                    return;
                }
            }
        }
        /// <inheritdoc/>
        public async Task <bool> TryAuthenticateAsync()
        {
            var refreshToken = GetRefreshToken();

            if (string.IsNullOrWhiteSpace(refreshToken))
            {
                IsAuthenticated = false;
                return(false);
            }

            var session = new AccountSession
            {
                ClientId     = SecurityHelper.ToUnsecureString(_clientId),
                RefreshToken = SecurityHelper.ToUnsecureString(SecurityHelper.DecryptString(refreshToken))
            };

            var msaAuthProvider = new MsaAuthenticationProvider(SecurityHelper.ToUnsecureString(_clientId), "https://login.live.com/oauth20_desktop.srf", _scope)
            {
                CurrentAccountSession = session
            };

            try
            {
                var httpProvider = new HttpProvider(new Serializer());
                httpProvider.OverallTimeout = TimeSpan.FromMinutes(20);

                _client = new OneDriveClient("https://api.onedrive.com/v1.0", msaAuthProvider, httpProvider);
                await msaAuthProvider.AuthenticateUserAsync();

                IsAuthenticated = msaAuthProvider.IsAuthenticated;
                TokenProvider.SetToken("RefreshToken", SecurityHelper.EncryptString(SecurityHelper.ToSecureString(msaAuthProvider.CurrentAccountSession.RefreshToken)));
                Logger.Instance.Information($"User authenticated to {CloudServiceName}.");
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                IsAuthenticated = false;
            }

            return(IsAuthenticated);
        }
Ejemplo n.º 10
0
        private async Task DoTestOneDrive()
        {
            var msaAuthProvider = new MsaAuthenticationProvider(
                ClientId,
                ReturnURI,
                new string[] { "onedrive.readwrite", "wl.signin" });
            await msaAuthProvider.AuthenticateUserAsync();

            var oneDriveClient = new OneDriveClient("https://api.onedrive.com/v1.0", msaAuthProvider);

            var drive = await oneDriveClient
                        .Drive
                        .Request()
                        .GetAsync();

            var rootItem = await oneDriveClient
                           .Drive
                           .Root
                           .Request()
                           .GetAsync();

            var items = await oneDriveClient.Drive.Root.Children.Request().GetAsync();

            var item1 = await oneDriveClient.Drive.Items["A13352E228C6410D!145135"].Request().GetAsync();

            var builder = oneDriveClient.Drive.Root.ItemWithPath("file.txt");
            var file    = await builder
                          .Request()
                          .GetAsync();

            var contentStream = await builder.Content
                                .Request()
                                .GetAsync();

            Debug.WriteLine($"Content for file {file.Name}:");
            using (var reader = new StreamReader(contentStream))
            {
                Debug.WriteLine(reader.ReadToEnd());
            }
        }