Example #1
0
        /// <summary>
        /// 2019/10/22
        /// Add a user to table
        /// usually used to registe
        /// </summary>
        /// <returns></returns>
        public async ValueTask <TUser> AddUserAsync(TUser user, bool isanonymous = false)
        {
            ValidatePassword(user.PassWordHash);
            if (string.IsNullOrEmpty(user.Email) ||
                string.IsNullOrEmpty(user.Name) ||
                string.IsNullOrEmpty(user.PassWordHash))
            {
                throw new _401Exception("register data should not be null!");
            }
            if (!isanonymous)
            {
                user.IsEmailConfirmed = false;
            }
            else
            {
                user.IsEmailConfirmed = true;
            }
            user.AvatarUrl = $"https://cdn.limfx.pro/img/ran/{Math.Abs(user.Id.GetHashCode()%993)+1}";
            var a = HashedPassword.New(user.PassWordHash);

            user.PassWordHash  = a.Hash + a.Salt;
            user.SecurityStamp = DateTime.UtcNow.ToString();
            try
            {
                await AddAsync(user);
            }
            catch (Exception e)
            {
                throw new _401Exception("Email has been taken!", e);
            }
            return(user);
        }
Example #2
0
        public static string HashPassword(string password, out string salt)
        {
            var hash = HashedPassword.New(password, hashLength: 50, saltLength: 10);

            salt = hash.Salt;
            return(hash.Hash);
        }
Example #3
0
        public void Empty()
        {
            var hash = HashedPassword.New(string.Empty);

            Assert.IsTrue(hash.Check(string.Empty));
            Assert.IsFalse(hash.Check(" "));
        }
Example #4
0
        public void DefaultLengths()
        {
            var hash = HashedPassword.New("foo");

            Assert.IsTrue(hash.Check("foo"));
            Assert.IsFalse(hash.Check("bar"));
        }
Example #5
0
        public void CustomLengths()
        {
            var hashLength = 100;
            var saltLength = 8;

            var hash = HashedPassword.New("foo", hashLength, saltLength);

            Assert.IsTrue(hash.Check("foo"));
            Assert.IsFalse(hash.Check("bar"));

            Assert.AreEqual(hashLength, hash.Hash.Length);
            Assert.AreEqual(saltLength, hash.Salt.Length);
        }
Example #6
0
        /// <summary>
        /// Adds or edits employee
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Event args</param>
        private void newBtn_Click(object sender, EventArgs e)
        {
            if (checkValid())
            {
                try
                {
                    //creates employee object and populates it with values
                    newEmployee                = new Employee();
                    newEmployee.FirstName      = firstNameTb.Text;
                    newEmployee.LastName       = lastNameTb.Text;
                    newEmployee.OIB            = (oibTb.Text);
                    newEmployee.UserName       = usernameTb.Text;
                    newEmployee.Email          = emailTb.Text;
                    newEmployee.PhoneNumber    = contactTb.Text;
                    newEmployee.Address        = addressTb.Text;
                    newEmployee.EmployeeTypeId = Convert.ToInt32(employeeTypeCb.SelectedValue);
                    newEmployee.IsActive       = aktivanChb.Checked;



                    if (!string.IsNullOrEmpty(passwordTb.Text))
                    {
                        //generates new password hash and salt from the entered plaintext password
                        var hashedPassword = HashedPassword.New(passwordTb.Text);
                        //encodes the hash and salt for storage in the database
                        var encodedHash = Base64Encode(hashedPassword.Hash);
                        var encodedSalt = Base64Encode(hashedPassword.Salt);

                        newEmployee.PasswordHash = encodedHash;
                        newEmployee.PasswordSalt = encodedSalt;
                    }

                    //checks if employee already exists, edits if it does
                    if (!string.IsNullOrEmpty(oibTb.Text))
                    {
                        var employeeByOib = this.employeeRepository.GetByOIB(oibTb.Text);
                        if (employeeByOib != null)
                        {
                            this.employeeRepository.Edit(newEmployee);
                            employeeDgv.Update();
                            employeeDgv.Refresh();
                            RefreshElements();
                            MessageBox.Show("Zaposlenik izmjenjen");
                        }
                        else
                        {
                            //if we are adding a new employee, the password must be entered as well
                            if (!string.IsNullOrEmpty(passwordTb.Text))
                            {
                                //adds new employee to database
                                this.employeeRepository.Add(newEmployee);
                                employeeDgv.Update();
                                employeeDgv.Refresh();
                                RefreshElements();
                                MessageBox.Show(@"Zaposlenik uspješno unesen");
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show(@"Neuspješan unos zaposlenika");
                    return;
                }
            }
            else
            {
                MessageBox.Show(@"Morate unijeti sve podatke");
            }
        }