Exemplo n.º 1
0
        async Task <bool> get_authorize()
        {
            var msaAuthProvider = new MsaAuthenticationProvider(
                MsaClientId,
                MsaReturnUrl,
                Scopes,
                new CredentialVault(MsaClientId)
                );

            try
            {
                await msaAuthProvider.RestoreMostRecentFromCacheOrAuthenticateUserAsync();

                oneDriveClient = new OneDriveClient("https://api.onedrive.com/v1.0", msaAuthProvider);
                return(true);
            }
            catch (ServiceException ex)
            {
                MessageBox.Show(
                    ex.Error.Message,
                    "Authentication failed",
                    MessageBoxButtons.OK);
                return(false);
            }
        }
Exemplo n.º 2
0
        //public async Task AuthenticateOneDrive()
        //{
        //    Exception error = null;
        //    if (this.oneDriveClient == null)
        //    {
        //        try
        //        {
        //            this.IsBusy = true;
        //            var msaAuthProvider = new MsaAuthenticationProvider(_clientId, _returnUrl, _scopes, /*CredentialCache*/ null, new CredentialVault(_clientId));
        //            await msaAuthProvider.RestoreMostRecentFromCacheOrAuthenticateUserAsync();
        //            oneDriveClient = new OneDriveClient(this.oneDriveConsumerBaseUrl, msaAuthProvider);

        //        }
        //        catch (Exception ex)
        //        {

        //        }
        //        if (error != null)
        //        {


        //        }
        //    }
        //}

        //public async void GetAppRoot()
        //{
        //    this.IsBusy = true;
        //    if (oneDriveClient == null)
        //    {
        //        AuthenticateOneDrive();
        //    }

        //    //await GetFolder(oneDriveClient.Drive.Special.AppRoot, true);
        //    var rootItem = await oneDriveClient.Drive.Root.Request().GetAsync();
        //    this.IsBusy = false;
        //}


        private async void InitializeClient()
        {
            var app = (App)Application.Current;

            if (app.OneDriveClient == null)
            {
                Task authTask;

                var msaAuthProvider = new MsaAuthenticationProvider(
                    this._clientId,
                    this._returnUrl,
                    _scopes,
                    new CredentialVault(this._clientId));
                authTask           = msaAuthProvider.RestoreMostRecentFromCacheOrAuthenticateUserAsync();
                app.OneDriveClient = new OneDriveClient(this.oneDriveConsumerBaseUrl, msaAuthProvider);
                app.AuthProvider   = msaAuthProvider;
                try
                {
                    await authTask;
                }
                catch (ServiceException exception)
                {
                    // Swallow the auth exception but write message for debugging.
                    Debug.WriteLine(exception.Error.Message);
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        ///     Perform an async Login Request
        /// </summary>
        public async Task <IOneDriveClient> LoginAsync()
        {
            try
            {
                var msaAuthenticationProvider = new MsaAuthenticationProvider(
                    ServiceConstants.MSA_CLIENT_ID,
                    ServiceConstants.RETURN_URL,
                    ServiceConstants.Scopes,
                    new CredentialVault(ServiceConstants.MSA_CLIENT_ID));

                await msaAuthenticationProvider.RestoreMostRecentFromCacheOrAuthenticateUserAsync();

                return(new OneDriveClient(ServiceConstants.BASE_URL, msaAuthenticationProvider));
            }
            catch (ServiceException serviceException)
            {
                Debug.WriteLine(serviceException);
                throw new BackupException("Authentication Failed with Graph.ServiceException", serviceException);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                throw new BackupException("Authentication Failed", ex);
            }
        }
Exemplo n.º 4
0
        private async void ConnectOneDriveServer()
        {
            //이미 연결중이면 수행하지 않음.
            if (IsConnecting)
            {
                return;
            }

            //현재 연결이 되어 있다면 연결 해제
            await DisconnectOneDriveServer(false);

            //연결중...
            IsConnecting     = true;
            ShowErrorMessage = false;

            var    resource      = ResourceLoader.GetForCurrentView();
            string errMsg        = resource.GetString("Message/Error/FailServerAuthentication"); //"인증에 실패하였습니다. 아이디/패스워드를 확인하세요. ";
            var    isOrderByName = _Sort == SortType.Name || _Sort == SortType.NameDescending;

            Task authTask = null;

            var msaAuthProvider = new MsaAuthenticationProvider(
                this.oneDriveConsumerClientId,
                this.oneDriveConsumerReturnUrl,
                this.scopes,
                new CredentialVault(this.oneDriveConsumerClientId));

            await msaAuthProvider.SignOutAsync();

            authTask = msaAuthProvider.RestoreMostRecentFromCacheOrAuthenticateUserAsync();

            OneDriveClient = new OneDriveClient(this.oneDriveConsumerBaseUrl, msaAuthProvider);
            AuthProvider   = msaAuthProvider;

            try
            {
                await authTask;

                //서버타입 설정
                ConnectedServerType = ServerTypes.OneDrive;

                await LoadOneDriveFoldersAsync();

                //인증 성공
                IsConnecting   = false;
                IsDisconnected = false;
            }
            catch (ServiceException exception)
            {
                // Swallow the auth exception but write message for debugging.
                Debug.WriteLine(exception.Error.Message);

                pathStack.Clear();
                IsConnecting   = false;
                IsDisconnected = true;
                ShowOrderBy    = false;
            }
        }
Exemplo n.º 5
0
        public async Task <bool> AuthenticateSaved(CancellationToken cs, string save)
        {
            msaAuthenticationProvider = new MsaAuthenticationProvider(MicrosoftSecret.ClientId, MicrosoftSecret.ClientSecret, "http://localhost:45674/authredirect", Scopes, null, new CredentialsVault(this, save));
            await msaAuthenticationProvider.RestoreMostRecentFromCacheOrAuthenticateUserAsync().ConfigureAwait(false);

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

            return(msaAuthenticationProvider.IsAuthenticated);
        }
        private async void InitializeClient(ClientType clientType, RoutedEventArgs e)
        {
            var app = (App)Application.Current;

            if (app.OneDriveClient == null)
            {
                Task authTask;

                if (clientType == ClientType.Business)
                {
                    var adalAuthProvider = new AdalAuthenticationProvider(
                        this.oneDriveForBusinessClientId,
                        this.oneDriveForBusinessReturnUrl);
                    authTask           = adalAuthProvider.AuthenticateUserAsync(this.oneDriveForBusinessBaseUrl);
                    app.OneDriveClient = new OneDriveClient(this.oneDriveForBusinessBaseUrl + "/_api/v2.0", adalAuthProvider);
                    app.AuthProvider   = adalAuthProvider;
                }
                else if (clientType == ClientType.ConsumerUwp)
                {
                    var onlineIdAuthProvider = new OnlineIdAuthenticationProvider(
                        this.scopes);
                    authTask           = onlineIdAuthProvider.RestoreMostRecentFromCacheOrAuthenticateUserAsync();
                    app.OneDriveClient = new OneDriveClient(this.oneDriveConsumerBaseUrl, onlineIdAuthProvider);
                    app.AuthProvider   = onlineIdAuthProvider;
                }
                else
                {
                    var msaAuthProvider = new MsaAuthenticationProvider(
                        this.oneDriveConsumerClientId,
                        this.oneDriveConsumerReturnUrl,
                        this.scopes,
                        new CredentialVault(this.oneDriveConsumerClientId));
                    authTask           = msaAuthProvider.RestoreMostRecentFromCacheOrAuthenticateUserAsync();
                    app.OneDriveClient = new OneDriveClient(this.oneDriveConsumerBaseUrl, msaAuthProvider);
                    app.AuthProvider   = msaAuthProvider;
                }

                try
                {
                    await authTask;
                    app.NavigationStack.Add(new ItemModel(new Item()));
                    this.Frame.Navigate(typeof(MainPage), e);
                }
                catch (ServiceException exception)
                {
                    // Swallow the auth exception but write message for debugging.
                    Debug.WriteLine(exception.Error.Message);
                }
            }
            else
            {
                this.Frame.Navigate(typeof(MainPage), e);
            }
        }
 /// <summary>
 /// Authenticate user to OneDrive.
 /// </summary>
 public async Task AuthenticateAsync()
 {
     try
     {
         oneDriveClient = new OneDriveClient("https://api.onedrive.com/v1.0", authProvider);
         await authProvider.RestoreMostRecentFromCacheOrAuthenticateUserAsync();
         await SetDriveAsync();
     }
     catch (ServiceException e)
     {
         Debug.WriteLine(e?.Error?.Message);
         oneDriveClient = null;
     }
 }
        private async Task <string> AuthenticateToProvider()
        {
            const string clientId = "1e872776-dbba-47e5-8035-3e2e4ab8a4e6"; // register this under https://apps.dev.microsoft.com/
            var          authenticationProvider = new MsaAuthenticationProvider(
                clientId,
                "https://login.live.com/oauth20_desktop.srf",               // this is the login-url for Microsoft
                new string[] { "wl.signin", "wl.basic" },                   // https://docs.microsoft.com/en-us/previous-versions/office/developer/onedrive-live-sdk-reference/dn631845(v=office.15)
                new CredentialVault(clientId));

            //TODO: In real app there are many more tests needed.
            // e.g. it is possible the restored Session from cache is valid but the attached accesstoken is already expired...
            await authenticationProvider.RestoreMostRecentFromCacheOrAuthenticateUserAsync();

            return(authenticationProvider.CurrentAccountSession.AccessToken);
        }
        public async Task InitAsync()
        {
            var authProvider = new MsaAuthenticationProvider(this.clientId, RETURN_URL, SCOPES, new CredentialVault(this.clientId));

            try
            {
                await authProvider.RestoreMostRecentFromCacheOrAuthenticateUserAsync();
            }
            catch (ServiceException e)
            {
                Debug.WriteLine("OneDrive auth error: " + e);
                throw new Exception($"OneDrive login failed: {e.Message}", e);
            }
            _oneDriveClient = new OneDriveClient(BASE_URL, authProvider);
        }
        public async Task Init()
        {
            try
            {
                string clientId = "<put your client id here>";

                MsaAuthenticationProvider msaAuthProvider =
                    new MsaAuthenticationProvider(clientId,
                                                  "https://login.live.com/oauth20_desktop.srf",
                                                  new string[] { "onedrive.readwrite", "wl.signin" },
                                                  new CredentialVault(clientId));
                await msaAuthProvider.RestoreMostRecentFromCacheOrAuthenticateUserAsync();

                oneDriveClient = new OneDriveClient("https://api.onedrive.com/v1.0",
                                                    msaAuthProvider);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }
Exemplo n.º 11
0
        private async Task <bool> SignIn()
        {
            Task authTask;

            var msaAuthProvider = new MsaAuthenticationProvider(
                msaClientId,
                null,
                msaReturnUrl,
                scopes,
                null,
                new CredentialVault(msaClientId));

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

            try
            {
                await authTask;
            }
            catch (ServiceException exception)
            {
                if (OAuthConstants.ErrorCodes.AuthenticationFailure == exception.Error.Code)
                {
                    MessageBox.Show(
                        "Authentication failed",
                        "Authentication failed",
                        MessageBoxButtons.OK);

                    this.oneDriveClient = null;
                }
                else
                {
                    PresentServiceException(exception);
                }
                return(false);
            }
            return(true);
        }
        /// <summary>
        ///     Perform an async Login Request
        /// </summary>
        public async Task<IOneDriveClient> LoginAsync()
        {
            try
            {
                var msaAuthenticationProvider = new MsaAuthenticationProvider(
                    ServiceConstants.MSA_CLIENT_ID,
                    ServiceConstants.RETURN_URL,
                    ServiceConstants.Scopes,
                    new CredentialVault(ServiceConstants.MSA_CLIENT_ID));

                await msaAuthenticationProvider.RestoreMostRecentFromCacheOrAuthenticateUserAsync();
                return new OneDriveClient(ServiceConstants.BASE_URL, msaAuthenticationProvider);
            }
            catch (ServiceException serviceException)
            {
                Debug.WriteLine(serviceException);
                throw new BackupException("Authentication Failed with Graph.ServiceException", serviceException);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                throw new BackupException("Authentication Failed", ex);
            }
        }
Exemplo n.º 13
0
        private async Task Initialize()
        {
            if (!_initialized)
            {
                _msaAuthProvider = new MsaAuthenticationProvider(OneDriveClientId, OneDriveRedirectUri, ControlAppPagesScopes, _credentials);
                bool success = await DispatcherHelper.ExecuteOnUIThreadAsync(async() =>
                {
                    try
                    {
                        await _msaAuthProvider.RestoreMostRecentFromCacheOrAuthenticateUserAsync();
                        return(true);
                    }
                    catch (Graph.ServiceException ex) when(ex.Error.Code == OAuthConstants.ErrorCodes.AuthenticationCancelled)
                    {
                        // Swallow it in this case. The user just cancelling is fine.
                        System.Diagnostics.Debug.WriteLine("Authentication cancelled by user.");
                        return(false);
                    }
                });

                if (!success)
                {
                    return;
                }
                _httpClient.DefaultRequestHeaders.Authorization = new HttpCredentialsHeaderValue("Bearer", _msaAuthProvider.CurrentAccountSession.AccessToken);

                ConnectedStatus = true;
                var validatedPageId = await ValidatePageId(_settingsService.OneNotePageId);

                _settingsService.OneNotePageId = validatedPageId.ValueOr(_settingsService.OneNotePageId);
                if (validatedPageId.HasValue)
                {
                    _initialized = true;
                }
            }
        }