Ejemplo n.º 1
0
        /// <summary>
        /// This method is used to prompt to login screen.
        /// </summary>
        /// <returns>True if sign in successfully, otherwise false</returns>
        public async Task <bool> SignInAsync()
        {
            var success = false;

            try
            {
                success = await GraphService.TryLoginAsync();
            }
            catch (Exception ex)
            {
                SignInFailed?.Invoke(this, new SignInFailedEventArgs(ex));
            }

            if (success)
            {
                AutomationProperties.SetName(this, string.Empty);

                Flyout = GenerateMenuItems();

                SignInCompleted?.Invoke(this, new SignInEventArgs()
                {
                    GraphClient = GraphService.GraphProvider
                });

                return(true);
            }

            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Tries to log in user if not already loged in
        /// </summary>
        /// <returns>true if service is already loged in</returns>
        internal async Task <bool> TryLoginAsync()
        {
            if (!IsInitialized)
            {
                return(false);
            }

            if (IsAuthenticated)
            {
                return(true);
            }

            try
            {
                await _readLock.WaitAsync();
                await LoginAsync();
            }
            catch (MsalServiceException ex)
            {
                // Swallow error in case of authentication cancellation.
                if (ex.ErrorCode != "authentication_canceled" &&
                    ex.ErrorCode != "access_denied")
                {
                    SignInFailed?.Invoke(this, new SignInFailedEventArgs(ex));
                }
            }
            finally
            {
                _readLock.Release();
            }

            return(IsAuthenticated);
        }
Ejemplo n.º 3
0
        private async void AuthenticateUserAsync(int id)
        {
            Reader user = new Reader(id, "name", "name", passwordField.Text);

            try
            {
                await RequestClient.Instance.PostItemAsync(user, "signin");
            }
            catch (BadHttpStatusCodeException ex)
            {
                if (ex.StatusCode == HttpStatusCode.NotFound)
                {
                    SignInFailed.Invoke(this, new EventArgs());
                    return;
                }
                else
                {
                    return;
                }
            }

            signInAttempts         = 0;
            progressBar.Visibility = ViewStates.Invisible;
            Intent intent = new Intent(this, typeof(TabbedPagesActivity));

            this.StartActivity(intent);
        }
Ejemplo n.º 4
0
        private void LoginButton_Click(object sender, EventArgs e)
        {
            progressBar.Visibility = ViewStates.Visible;
            if (signInAttempts >= 5)
            {
                SignInFailed += SignInFailed_Exit;
            }

            if (!int.TryParse(userIdField.Text, out int id))
            {
                SignInFailed.Invoke(this, new EventArgs());
                return;
            }
            AuthenticateUserAsync(id);
        }
Ejemplo n.º 5
0
        internal async Task <bool> ConnectForAnotherUserAsync()
        {
            if (!IsInitialized)
            {
                throw new InvalidOperationException("Microsoft Graph not initialized.");
            }

            try
            {
                var publicClientApplication = new PublicClientApplication(AppClientId);
                AuthenticationResult result = await publicClientApplication.AcquireTokenAsync(DelegatedPermissionScopes);

                var signedUser = result.User;

                foreach (var user in publicClientApplication.Users)
                {
                    if (user.Identifier != signedUser.Identifier)
                    {
                        publicClientApplication.Remove(user);
                    }
                }

                await LoginAsync();

                return(true);
            }
            catch (MsalServiceException ex)
            {
                // Swallow error in case of authentication cancellation.
                if (ex.ErrorCode != "authentication_canceled" &&
                    ex.ErrorCode != "access_denied")
                {
                    SignInFailed?.Invoke(this, new SignInFailedEventArgs(ex));
                }
            }

            return(false);
        }
Ejemplo n.º 6
0
 private void GraphService_SignInFailed(object sender, Services.MicrosoftGraph.SignInFailedEventArgs e)
 {
     SignInFailed?.Invoke(sender, new SignInFailedEventArgs(e.Exception));
 }