private async void CreateAccountButton_OnClicked(object sender, EventArgs e)
        {
            try
            {
                if (CreatePasswordEntry.Text.Equals(CreatePasswordConfirmationEntry.Text))
                {
                    PasswordController passwordController = new PasswordController();
                    IUserRestService   userRestService    = new UserRestService();

                    User user = new User();
                    user.UserName     = CreateUserNameEntry.Text;
                    user.Salt         = passwordController.GenerateSalt();
                    user.HashPassword = passwordController.GenerateHashedPassword(CreatePasswordEntry.Text, Encoding.ASCII.GetBytes(user.Salt));
                    user.UserType     = new UserType
                    {
                        TypeName = "personal"
                    };

                    await userRestService.Create(user);

                    //Debug.WriteLine("Hashes and salt be here: " + user.HashPassword + " " + user.Salt);
                }
                else
                {
                    await DisplayAlert("Fejl", "Passwords are not equals", "OK");
                }
            }
            catch (FaultException <Exception> exc)
            {
                await DisplayAlert("Fejl", exc.Message, "OK");
            }
        }
Пример #2
0
        public App()
        {
            InitializeComponent();

            filmService      = new FilmRestService();
            actorService     = new ActorRestService();
            filmMakerService = new FilmMakerRestService();
            userService      = new UserRestService();
            loginService     = new LoginRestService();

            MainPage = new NavigationPage(new LoginPage());
        }
        private async void SignInButton_OnClicked(object sender, EventArgs e)
        {
            try
            {
                var uName = UserNameEntry.Text;
                var pWord = PasswordEntry.Text;
                PasswordController pCtrl = new PasswordController();
                bool status = await pCtrl.VerifyLogin(uName, pWord);

                if (status)
                {
                    IUserRestService restService = new UserRestService();
                    User             user        = await restService.FindByUserName(uName);

                    if (user != null)
                    {
                        _tabbedMapMainPage = new TabbedMapMainPage
                        {
                            User = user
                        };
                        _tabbedMapMainPage.StartUpWithUser();
                        await Navigation.PushAsync(_tabbedMapMainPage);

                        Navigation.RemovePage(this);
                    }
                }
            }
            catch (EmptyInputException exc)
            {
                await DisplayAlert("Fejl", exc.ReturnMessage, "OK");
            }
            catch (UserOrPasswordException exc)
            {
                await DisplayAlert("Fejl", exc.ReturnMessage, "OK");
            }
            catch (FaultException <UserOrPasswordException> exc)
            {
                await DisplayAlert("Fejl", exc.Message, "OK");
            }
            catch (FaultException <UserNotFoundException> exc)
            {
                await DisplayAlert("Fejl", exc.Message, "OK");
            }
            catch (FaultException <Exception> exc)
            {
                await DisplayAlert("Fejl", exc.Message, "OK");
            }
            catch (Exception exc)
            {
                await DisplayAlert("Fejl", exc.Message, "OK");
            }
        }
        /// <summary>
        /// Checks if the user exist in database with password
        /// </summary>
        /// <param name="username">string</param>
        /// <param name="password">string</param>
        /// <returns>Task<bool/></returns>
        public async Task <bool> VerifyLogin(string username, string password)
        {
            bool status;

            if (username != null && password != null &&
                username.Length != 0 && password.Length != 0)
            {
                IUserRestService userRestService = new UserRestService();
                string           salt            = await userRestService.FindSaltByUserName(username);

                byte[] theSalt       = Encoding.UTF8.GetBytes(salt);
                string hashPassword  = GenerateHashedPassword(password, theSalt);
                User   userToCompare = new User {
                    UserName = username, HashPassword = hashPassword
                };
                status = await userRestService.CompareHashes(userToCompare);
            }
            else
            {
                throw new EmptyInputException();
            }

            return(status);
        }
        private async Task ProcessLoging(object parameter)
        {
            IRestDataMapper mapper = containerExtension.Resolve <IRestDataMapper>();

            if (SelectedBranch == null || UserName == null)
            {
                Warrning = "Invalid Branch Or User Name";
                return;
            }
            if (parameter is IHavePassword havePassword)
            {
                string userPassword = havePassword.Password;
                if (string.IsNullOrEmpty(userPassword))
                {
                    Warrning = "Invalid Password Or User Name";
                    return;
                }

                Branch b    = branches.Where(br => br.name.Equals(SelectedBranch)).FirstOrDefault();
                User   user = await UserRestService.GetUserByParamAndBranchIdAsync(mapper, UserName, b.id);

                if (user != null)
                {
                    if (user.id > 0)
                    {
                        if (userPassword.ComparePassword(user.password))
                        {
                            IApplicationController controller = containerExtension.Resolve <IApplicationController>();
                            await controller.LogingApplication(new LoggedUserProvider(user));
                        }
                        else
                        {
                            Warrning = "Invalid Password Or User Name";
                            UserName = string.Empty;
                            havePassword.ClearPassword();
                            return;
                        }
                    }
                    else
                    {
                        Warrning = "Invalid Password Or User Name";
                        UserName = string.Empty;
                        havePassword.ClearPassword();
                        return;
                    }
                }
                else
                {
                    Warrning = "Invalid Password Or User Name";
                    UserName = string.Empty;
                    havePassword.ClearPassword();
                    return;
                }
            }
            else
            {
                Warrning = "Internal Error Please Contact Developer.";
                return;
            }
            await Task.Delay(1000);
        }