public string Create(string username)
        {
            var currentUsername = (User as UserModel).Username.ToLower();

            if (AdminUsers.Contains(currentUsername) == false)
            {
                return("Bạn không có quyền truy cập");
            }
            using (var dbContext = new DataContext())
            {
                string newPass = HashPassword.Hash(username, "123456a@");
                var    user    = dbContext.Users.FirstOrDefault(e => e.Username.ToLower() == username);
                if (user != null)
                {
                    return("Người dùng đã tồn tại");
                }
                user = new User()
                {
                    Username  = username,
                    Password  = newPass,
                    Email     = username + "@ists.com.vn",
                    TrustedIP = "localhost"
                };
                dbContext.Users.Add(user);
                dbContext.SaveChanges();
            }
            return("Thành công");
        }
 public string Hash(string username, string password)
 {
     if (AdminUsers.Contains((User as UserModel).Username.ToLower()))
     {
         return(HashPassword.Hash(username, password));
     }
     else
     {
         return("Bạn không có quyền truy cập chức năng này");
     }
 }
        public async Task <ActionResult <Models.WorkerWithToken> > SignUp([FromBody] Models.RegisterModel registerModel)
        {
            var registerModelValidator = new RegisterModelValidator();
            var validationResult       = registerModelValidator.Validate(registerModel);

            if (!validationResult.IsValid)
            {
                return(UnprocessableEntity(validationResult.Errors.Select(x => new ValidatorException
                {
                    Field = x.PropertyName,
                    Message = x.ErrorCode,
                    Description = x.ErrorMessage
                }).ToList()));
            }

            var hashPassword = HashPassword.Hash(registerModel.Password);
            var existUser    = await _context.Workers.SingleOrDefaultAsync(x => x.Email == registerModel.Email && x.Password == hashPassword);

            if (existUser == null)
            {
                var newWorker = new Models.Worker
                {
                    FirstName    = registerModel.FirstName,
                    LastName     = registerModel.LastName,
                    Email        = registerModel.Email,
                    Password     = hashPassword,
                    SpecialityId = registerModel.SpecialityId
                };
                var token = JWTExtension.CreateToken(newWorker);
                _context.Workers.Add(newWorker);
                await _context.SaveChangesAsync();

                var registeredWorker = await _context.Workers
                                       .Include(x => x.Speciality)
                                       .ThenInclude(x => x.Department)
                                       .SingleOrDefaultAsync(x => x.Email == newWorker.Email && x.Password == newWorker.Password);

                return(Ok(new Models.WorkerWithToken
                {
                    Token = token,
                    Worker = ResponseWorkerList.FromApiWorker(registeredWorker, avatar: _getAvatar(registeredWorker.AvatarUrl)),
                }));
            }
            else
            {
                return(Conflict(new ExceptionInfo
                {
                    Message = AuthErrorsMessages.UserExist,
                    Description = "Сотрудник с таким Email и паролем уже существует"
                }));
            }
        }
Beispiel #4
0
        public bool CheckCredentials(CredentialsDTO credentials)
        {
            User user = _userDal.GetByLogin(credentials.Login);

            if (user.HashPassword == HashPassword.Hash(credentials.Password))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public async Task <ActionResult <Models.WorkerWithToken> > SignIn([FromBody] Models.AuthModel authModel)
        {
            var authModelValidator = new AuthModelValidator();
            var validationResult   = authModelValidator.Validate(authModel);

            if (!validationResult.IsValid)
            {
                return(UnprocessableEntity(validationResult.Errors.Select(x => new ValidatorException {
                    Field = x.PropertyName,
                    Message = x.ErrorCode,
                    Description = x.ErrorMessage
                }).ToList()));
            }

            var hashPassword = HashPassword.Hash(authModel.Password);
            var existUser    = _context.Workers
                               .Include(x => x.Speciality)
                               .ThenInclude(x => x.Department)
                               .SingleOrDefault(x => x.Email == authModel.Email && x.Password == hashPassword);

            if (existUser != null)
            {
                Models.File avatar;


                if (existUser.AvatarUrl != null)
                {
                    var fullFileName = existUser.AvatarUrl.Replace(Constans.ApiUrl + Constans.FileDownloadPart, string.Empty);
                    avatar = _context.Files.SingleOrDefault(x => x.Name == fullFileName);
                }
                else
                {
                    avatar = null;
                }


                var token = JWTExtension.CreateToken(existUser);
                return(Ok(new Models.WorkerWithToken {
                    Token = token,
                    Worker = ResponseWorkerList.FromApiWorker(existUser, avatar: avatar),
                }));
            }
            else
            {
                return(NotFound(new ExceptionInfo {
                    Message = AuthErrorsMessages.UserNotFound,
                    Description = "Сотрудник с таким Email и паролем не существует"
                }));
            }
        }
        public string ChangePass(string password)
        {
            var username = (User as UserModel).Username.ToLower();

            using (var dbContext = new DataContext())
            {
                string newPass = HashPassword.Hash(username, password);
                var    user    = dbContext.Users.FirstOrDefault(e => e.Username.ToLower() == username);
                if (user == null)
                {
                    return("User không hợp lệ");
                }
                user.Password = newPass;
                dbContext.SaveChanges();
            }
            return("Thành công");
        }
        public IActionResult RegisterUser(UserViewmodel userViewModel)
        {
            var users = _userService.GetAll().ToArray();

            if (userViewModel.Password != userViewModel.ConfirmPassword)
            {
                return(BadRequest("Lösenorden matchar inte."));
            }
            bool emailExist = _authControllerServices.CheckMailAddress(users, userViewModel);

            if (emailExist == false)
            {
                var user = Mapper.ViewModelToModelMapping.UserViewModelToUser(userViewModel);
                user.Password = _hashPassword.Hash(userViewModel.Password);
                _userService.Add(user);
                return(Ok("Användaren har sparats, du skickas till login sidan inom 5 sekunder!"));
            }
            return(BadRequest("Mailadressen är redan registerad."));
        }
        public string ResetPass(string username)
        {
            var currentUsername = (User as UserModel).Username.ToLower();

            if (AdminUsers.Contains(currentUsername) == false)
            {
                return("Bạn không có quyền truy cập");
            }
            using (var dbContext = new DataContext())
            {
                string newPass = HashPassword.Hash(username, "123456a@");
                var    user    = dbContext.Users.FirstOrDefault(e => e.Username.ToLower() == username);
                if (user == null)
                {
                    return("User không hợp lệ");
                }
                user.Password = newPass;
                dbContext.SaveChanges();
            }
            return("Thành công");
        }
Beispiel #9
0
        private void button_create_Click(object sender, EventArgs e)
        {
            User user = new User();

            if (textbox_pwd.Text == textbox_pwd2.Text)
            {
                user.HashPassword = HashPassword.Hash(textbox_pwd2.Text);
            }
            else
            {
                MessageBox.Show(
                    "Passwords are not match!",
                    "Error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                return;
            }
            user.Name  = textbox_name.Text;
            user.Login = textbox_login.Text;
            user.Age   = Convert.ToInt32(textbox_age.Text);
            try
            {
                _userService.CreateUser(user);
                MessageBox.Show(
                    "New user was created!",
                    "Information",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
                this.Close();
            }
            catch
            {
                MessageBox.Show(
                    "You have troubles!",
                    "Error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                return;
            }
        }
 public ActionResult Login(UserModel user)
 {
     if (ModelState.IsValid)
     {
         using (var dbContext = new DataContext())
         {
             var hashPassword = HashPassword.Hash(user.Username.ToLower(), user.Password);
             var logonUser    = dbContext.Users
                                .FirstOrDefault(u => u.Username.ToLower() == user
                                                .Username.ToLower() && u
                                                .Password == hashPassword);
             if (logonUser != null)
             {
                 user.Id       = logonUser.Id;
                 user.Password = null;
                 SetAuthCookie(user);
                 return(RedirectToAction("Index", "Home"));
             }
         }
     }
     ModelState.AddModelError("", "invalid Username or Password");
     return(View());
 }
            public async Task <ApplicationUser> Handle(UpdateApplicationUserCommand request, CancellationToken cancellationToken)
            {
                var ApplicationUser = await _applicationDbContext.ApplicationUsers.FindAsync(request.Id);

                if (ApplicationUser == null)
                {
                    throw new NotFoundException(nameof(ApplicationUser), request.Id);
                }

                using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    ApplicationUser.Id       = request.Id;
                    ApplicationUser.UserName = request.UserName;
                    ApplicationUser.FullName = request.FullName;

                    ApplicationUser.PasswordHash = HashPassword.Hash(request.Password);

                    _applicationDbContext.ApplicationUsers.Update(ApplicationUser);
                    await _applicationDbContext.SaveChangesAsync(cancellationToken);

                    scope.Complete();
                }
                return(ApplicationUser);
            }
Beispiel #12
0
 public string GetHashedPassword(string password)
 {
     return(HashPassword.Hash(password));
 }