示例#1
0
        public async Task <IUserModel> CreateAsync(IUserModel model, string userPassword)
        {
            DAL.Entities.User newUser = mapper.Map <DAL.Entities.User>(model);

            if (string.IsNullOrWhiteSpace(userPassword))
            {
                throw new AppException("Password is required");
            }


            if (await userRepository.CheckIfExistAsync(model.Username))
            {
                throw new AppException("Username \"" + model.Username + "\" is already taken");
            }

            byte[] passwordHash, passwordSalt;
            CreatePasswordHash(userPassword, out passwordHash, out passwordSalt);

            newUser.Id           = Guid.NewGuid();
            newUser.DateCreated  = DateTime.Now;
            newUser.DateUpdated  = DateTime.Now;
            newUser.PasswordHash = passwordHash;
            newUser.PasswordSalt = passwordSalt;

            //TODO user roles
            //var userRole = new UserRole
            //{
            //    Id = Guid.NewGuid(),
            //    DateUpdated = DateTime.Now,
            //    DateCreated = DateTime.Now,
            //    Name = Role.User,
            //    Abrv = Role.User,
            //    UserId = newUser.Id,
            //};

            //newUser.UserRole = userRole;
            await userRepository.CreateAsync(newUser);

            return(mapper.Map <IUserModel>(newUser));
        }
示例#2
0
        public async Task <OperationDetails> Create(UserDTO userDto)
        {
            DAL.Entities.User user = await uow.UserManager.FindByEmailAsync(userDto.Email);

            if (user == null)
            {
                user = new DAL.Entities.User {
                    Email = userDto.Email, UserName = userDto.Email
                };
                var result = await uow.UserManager.CreateAsync(user, userDto.Password);

                if (result.Errors.Count() > 0)
                {
                    return(new OperationDetails(false, result.Errors.FirstOrDefault(), ""));
                }
                // добавляем роль
                var role = uow.RoleManager.FindByName(userDto.Role);
                if (role == null)
                {
                    userDto.Role = "RegisteredUser";
                }
                try
                {
                    var res = await uow.UserManager.AddToRoleAsync(user.Id, userDto.Role);

                    await uow.SaveChangesAsync();
                }
                catch (Exception)
                {
                    throw;
                }

                return(new OperationDetails(true, "Регистрация успешно пройдена", ""));
            }
            else
            {
                return(new OperationDetails(false, "Пользователь с таким логином уже существует", "Email"));
            }
        }
示例#3
0
        public async Task <bool> Register(RegisterViewModel model, ModelStateDictionary modelState)
        {
            if (!modelState.IsValid)
            {
                return(false);
            }

            var user   = new DAL.Entities.User(model.Email);
            var result = await _userManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)
            {
                foreach (var error in result.Errors)
                {
                    modelState.AddModelError(string.Empty, error.Description);
                    return(false);
                }
            }

            await _signInManager.SignInAsync(user, false);

            return(true);
        }
示例#4
0
 public static Models.User Map(DAL.Entities.User user)
 => new User
 {
     Name = user.UserName, Id = user.Id, Email = user.Email, IsApproved = user.IsApproved
 };