Пример #1
0
 public void SetPassword(User user, string password)
 {
     user = _userGR.Find(user.Id);
     if (user == null)
     {
         throw new ClientException("u-inc-user-id");
     }
     user.PasswordHash = PasswordHandler.CreatePasswordHash(password);
     _userGR.Update(user);
 }
Пример #2
0
        public async Task SetPasswordAsync(User user, string password)
        {
            user = await _userGR.FindAsync(user.Id);

            if (user == null)
            {
                throw new ClientException("u-inc-user-id");
            }
            user.PasswordHash = PasswordHandler.CreatePasswordHash(password);
            await _userGR.UpdateAsync(user);
        }
Пример #3
0
 public static void OnFirstInit(IGenericRepository <User> userGR, IGenericRepository <Role> roleGR)
 {
     userGR.Create(new User
     {
         Id           = "admin",
         Name         = "Admin",
         Surname      = "Admin",
         Lastname     = "Admin",
         Email        = "*****@*****.**",
         Login        = "******",
         PhotoId      = "default-user-photo",
         PasswordHash = PasswordHandler.CreatePasswordHash("admin"),
         RoleId       = roleGR.GetAll(x => x.Name == "Admin").FirstOrDefault().Id
     });
 }
Пример #4
0
        public async Task <LoggedDto> EndRegister(EndRegisterDto entity, string uuid)
        {
            var token = ValidateRegistrationToken(entity.RegistrationToken);

            if (await _userService.IsIssetAsync(token.Id))
            {
                throw new ClientException("r-token-already-used");
            }
            else if (_userService.IsIssetByLogin(entity.Login))
            {
                throw new ClientException("r-login-alrd-reg");
            }
            else if (_userService.IsIssetByEmail(token.Claims.First(x => x.Type == "email").Value))
            {
                throw new ClientException("r-email-alrd-reg");
            }

            var user = new User
            {
                Id           = token.Id,
                Login        = entity.Login,
                Email        = token.Claims.First(x => x.Type == "email").Value,
                Name         = entity.Name,
                Surname      = entity.Surname,
                Lastname     = entity.Lastname,
                PhotoId      = "default-user-photo",
                Birthday     = entity.Birthday.Value,
                RoleId       = int.Parse(token.Claims.First(x => x.Type == "role").Value),
                PasswordHash = PasswordHandler.CreatePasswordHash(entity.Password)
            };

            var noPermissions = new List <Permission>();

            if (token.Claims.First(x => x.Type == "noPermissions").Value.Length > 0)
            {
                foreach (var item in token.Claims.First(x => x.Type == "noPermissions").Value.Split(' '))
                {
                    noPermissions.Add(new Permission {
                        Id = int.Parse(item)
                    });
                }
            }
            var usr = await _userService.AddAsync(user, noPermissions);

            usr.Role = await _roleService.GetAsync(usr.RoleId);

            return(await _loginService.Login(usr, uuid));
        }
Пример #5
0
        public ActionResult Create([Bind(Include = "Id,UserID,CompanyID,FirstName,LastName,OtherName,Phone,Password,Address,DoB,NextOfKin,NextOfKinNumber,SbDate,SbExpiry,TimeStamp")] Rider rider)
        {
            rider.TimeStamp = DateTime.Now;

            if (ModelState.IsValid)
            {
                rider.Password = PasswordHandler.CreatePasswordHash(rider.Password);
                rider.SbDate   = DateTime.Now;
                rider.SbExpiry = DateTime.Now;
                db.Riders.Add(rider);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(rider));
        }
Пример #6
0
        public void  Authenticate()
        {
            byte[] passwordHash, passwordSalt;
            passwordHandler.CreatePasswordHash(password, out passwordHash, out passwordSalt);
            var repositoryMock = new Mock <IAccountRepository <Account> >();

            repositoryMock.Setup(x => x.Get(account.Email)).Returns(new Account()
            {
                PasswordHash = passwordHash, PasswordSalt = passwordSalt
            });
            AccountManager <Account> accountManager = new AccountManager <Account>(passwordHandler, new LoggerFactory());

            Account result = accountManager.Authenticate(account.Email, password, repositoryMock.Object);

            Assert.NotNull(result);
        }
Пример #7
0
        /// <summary>
        /// Method to create new users
        /// </summary>
        /// <param name="user"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public Users Create(Users user, string password)
        {
            // Validate password
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new AppException("Password is required");
            }

            if (_context.Users.Any(x => x.UserName == user.UserName))
            {
                throw new AppException("Username \"" + user.UserName + "\" is already taken");
            }

            byte[] passwordHash, passwordSalt;
            PasswordHandler.CreatePasswordHash(password, out passwordHash, out passwordSalt);

            _context.Users.Add(user);
            _context.SaveChanges();

            return(user);
        }
Пример #8
0
        /// <summary>
        /// Method for updating users
        /// </summary>
        /// <param name="userParam"></param>
        /// <param name="password"></param>
        public void Update(Users userParam, string password = null)
        {
            // Find user by id
            var user = _context.Users.Find(userParam.Id);

            // If there is no user throw app exception
            if (user == null)
            {
                throw new AppException("User not found");
            }

            if (userParam.UserName != user.UserName)
            {
                // Username has changed so check if the new username is already taken
                if (_context.Users.Any(x => x.UserName == userParam.UserName))
                {
                    throw new AppException("Username " + userParam.UserName + " is already taken");
                }
            }

            // Update user properties
            user.FirstName   = userParam.FirstName;
            user.LastName    = userParam.LastName;
            user.Email       = userParam.Email;
            user.PhoneNumber = userParam.PhoneNumber;
            user.UserName    = userParam.UserName;

            // update password if it was entered
            if (!string.IsNullOrWhiteSpace(password))
            {
                byte[] passwordHash, passwordSalt;
                PasswordHandler.CreatePasswordHash(password, out passwordHash, out passwordSalt);

                //user.PasswordHash = ;
            }

            _context.Users.Update(user);
            _context.SaveChanges();
        }
Пример #9
0
 // Method to hash a password
 private string HashPassword(string password)
 {
     return(PasswordHandler.CreatePasswordHash(password));
 }
Пример #10
0
 private void btnhash_Click(object sender, EventArgs e)
 {
     txtlog.Text += "\r\n > " + PasswordHandler.CreatePasswordHash(txtpassword.Text);
 }
Пример #11
0
        public IActionResult Register(Registration registration)
        {
            if (ModelState.IsValid)
            {
                User existingUser = _context.Users.Where(u => u.Email == registration.Email).FirstOrDefault();
                if (existingUser == null)
                {
                    if (registration.Password1 == registration.Password2)
                    {
                        User user = new User
                        {
                            Email    = registration.Email,
                            Password = PasswordHandler.CreatePasswordHash(registration.Password1)
                        };

                        _context.Users.Add(user);
                        _context.SaveChanges();

                        if (registration.RegistrationType == "Applicant")
                        {
                            Applicant applicant = new Applicant
                            {
                                Name          = registration.Name,
                                Country       = registration.Country,
                                City          = registration.City,
                                StreetAddress = registration.StreetAddress,
                                UserId        = user.Id
                            };

                            _context.Applicants.Add(applicant);
                            _context.SaveChanges();
                        }
                        else if (registration.RegistrationType == "Manufacturer")
                        {
                            Manufacturer manufacturer = new Manufacturer
                            {
                                Name          = registration.Name,
                                Country       = registration.Country,
                                City          = registration.City,
                                StreetAddress = registration.StreetAddress,
                                UserId        = user.Id
                            };

                            _context.Manufacturers.Add(manufacturer);
                            _context.SaveChanges();
                        }

                        return(RedirectToAction("Login", "User"));
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, "Passwords aren't the same");
                    }
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Email is already taken");
                }
            }
            return(View(registration));
        }