Exemple #1
0
        private async Task <UserReturnObject> Authenticate(UserAuthentication userAuthentication)
        {
            UserReturnObject userReturnObject = new UserReturnObject();

            using (var httpClient = new HttpClient())
            {
                //httpClient.DefaultRequestHeaders.Add("Key", "Secret@123");
                StringContent content = new StringContent(JsonConvert.SerializeObject(userAuthentication), Encoding.UTF8, "application/json");

                using (var response = await httpClient.PostAsync($"{Config.BaseUrl}/api/users/authenticate", content))
                {
                    string apiResponse = await response.Content.ReadAsStringAsync();

                    try
                    {
                        userReturnObject = JsonConvert.DeserializeObject <UserReturnObject>(apiResponse);
                    }
                    catch (Exception ex)
                    {
                        //ViewBag.Result = apiResponse;
                        //return View();
                    }
                }
            }
            return(userReturnObject);
        }
        private async Task <UserReturnObject> Authenticate(UserAuthentication userAuthentication)
        {
            UserReturnObject userReturnObject = new UserReturnObject();

            userReturnObject = await PostRequest <UserReturnObject>("/api/users/authenticate", userAuthentication);

            return(userReturnObject);
        }
Exemple #3
0
        public async Task <UserReturnObject> AuthenticateUser(string username, string clearTextPassword)
        {
            var hashedPassword = Helpers.Helper.GenerateSHA256String(clearTextPassword);
            UserAuthentication userAuthentication = new UserAuthentication
            {
                UserName = username,
                Password = hashedPassword
            };

            UserReturnObject userReturnObject = await Authenticate(userAuthentication);

            return(userReturnObject);
        }
Exemple #4
0
        private async void Login(object parameter)
        {
            PasswordBox passwordBox       = parameter as PasswordBox;
            string      clearTextPassword = passwordBox.Password;

            try
            {
                Status = string.Empty;
                App.SetCursorToWait();
                //Validate credentials through the authentication service
                if (string.IsNullOrEmpty(Username))
                {
                    Username = string.Empty;
                }

                if (string.IsNullOrEmpty(clearTextPassword))
                {
                    clearTextPassword = string.Empty;
                }

                UserReturnObject user = await _authenticationService.AuthenticateUser(Username, clearTextPassword);

                if (user == null)
                {
                    Status = "Incorrect Username or Password.  Please try again.";
                    passwordBox.Password = string.Empty;
                    Username             = string.Empty;
                    App.SetCursorToArrow();
                    OnRequestFocus?.Invoke(this, new EventArgs());
                    return;
                }

                //Get the current principal object
                CustomPrincipal customPrincipal  = App.CurrentPrincipal as CustomPrincipal;
                var             currentPrincipal = Thread.CurrentPrincipal;

                if (customPrincipal == null)
                {
                    throw new ArgumentException("The application's default thread principal must be set to a CustomPrincipal object on startup.");
                }

                //Authenticate the user
                customPrincipal.Identity = new CustomIdentity(user.UserName, user.Roles);

                _mainWindowViewModel.IsLoggedIn = true;
                LoggedInName = $"{user.FirstName} {user.LastName}";
                _mainWindowViewModel.LoggedInUserInfo = new Controls.Models.UserInfo()
                {
                    Username     = LoggedInName,
                    Roles        = string.Join(", ", user.Roles),
                    FullUserInfo = user
                };

                //Update UI
                OnPropertyChanged("AuthenticatedUser");
                OnPropertyChanged("IsAuthenticated");

                _loginCommand.RaiseCanExecuteChanged();
                _logoutCommand.RaiseCanExecuteChanged();
                Username             = string.Empty; //reset
                passwordBox.Password = string.Empty; //reset
                Status = string.Empty;
                App.SetCursorToArrow();
                _navigationService.NavigateTo(Enums.PageKey.Dashboard);

                //  dashboard.Show();

                OnRequestClose?.Invoke(this, new EventArgs());
            }
            catch (UnauthorizedAccessException)
            {
                App.SetCursorToArrow();

                Status = "Please enter valid admin name and password.";
                passwordBox.Password = string.Empty;
                Username             = string.Empty;
                OnRequestFocus?.Invoke(this, new EventArgs());
            }
            catch (Exception ex)
            {
                App.SetCursorToArrow();
#if DEBUG
                _mainWindowViewModel.Status = string.Format("ERROR: {0}", ex.Message);
#else
                _mainWindowViewModel.Status = string.Format("ERROR: Some error occurred please try again.");
#endif
            }
        }
        private async void Login(object parameter)
        {
            PasswordBox passwordBox       = parameter as PasswordBox;
            string      clearTextPassword = passwordBox.Password;

            try
            {
                IView dashboard = new Dashboard();
                // Allow user to login if username and password both empty
                if (string.IsNullOrEmpty(Username) && string.IsNullOrEmpty(clearTextPassword))
                {
                    dashboard.Show();

                    OnRequestClose?.Invoke(this, new EventArgs());
                    return;
                }

                App.Current.MainWindow.Cursor = Cursors.Wait;
                //Validate credentials through the authentication service
                UserReturnObject user = await _authenticationService.AuthenticateUser(Username, clearTextPassword);

                //Get the current principal object
                CustomPrincipal customPrincipal  = App.CurrentPrincipal as CustomPrincipal;
                var             currentPrincipal = Thread.CurrentPrincipal;

                if (customPrincipal == null)
                {
                    throw new ArgumentException("The application's default thread principal must be set to a CustomPrincipal object on startup.");
                }

                //Authenticate the user
                customPrincipal.Identity = new CustomIdentity(user.UserName, user.Roles);

                IsLoggedIn   = true;
                LoggedInName = $"{user.FirstName} {user.LastName}";

                //Update UI
                OnPropertyChanged("AuthenticatedUser");
                OnPropertyChanged("IsAuthenticated");

                _loginCommand.RaiseCanExecuteChanged();
                _logoutCommand.RaiseCanExecuteChanged();
                Username             = string.Empty; //reset
                passwordBox.Password = string.Empty; //reset
                Status = string.Empty;
                App.Current.MainWindow.Cursor = Cursors.Arrow;

                //  dashboard.Show();

                OnRequestClose?.Invoke(this, new EventArgs());
            }
            catch (UnauthorizedAccessException)
            {
                App.Current.MainWindow.Cursor = Cursors.Arrow;

                Status = "Please enter valid admin name and password.";
                passwordBox.Password = string.Empty;
                Username             = string.Empty;
                OnRequestFocus?.Invoke(this, new EventArgs());
            }
            catch (Exception ex)
            {
                App.Current.MainWindow.Cursor = Cursors.Arrow;
                Status = string.Format("ERROR: {0}", ex.Message);
            }
        }