Exemplo n.º 1
0
        /// <summary>
        /// Creates user, if it does not exist.
        /// Adds a role to the created user and creates user profile.
        /// </summary>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="AccountException"></exception>
        public async Task Create(UserDTO userDto)
        {
            if (userDto == null)
            {
                throw new ArgumentNullException(nameof(userDto));
            }

            ApplicationUser user = await identityUnitOfWork.UserManager.FindByEmailAsync(userDto.Email);

            if (user == null)
            {
                user = new ApplicationUser {
                    Email = userDto.Email, UserName = userDto.UserName
                };
                await identityUnitOfWork.UserManager.CreateAsync(user, userDto.Password);

                // add role
                await identityUnitOfWork.UserManager.AddToRoleAsync(user.Id, userDto.Role);

                // create user profile
                UserProfileEntity userProfile = new UserProfileEntity {
                    Id = user.Id
                };
                employeeUnitOfWork.Profile.Insert(userProfile);

                await identityUnitOfWork.SaveAsync();

                await employeeUnitOfWork.SaveAsync();
            }
            else
            {
                throw new AccountException("User with such email already exists.");
            }
        }
Exemplo n.º 2
0
        public async Task <OperationDetails> Create(AppUser appUser)
        {
            User _user = await _unitOfWork.ApplicationUserManager.FindByEmailAsync(appUser.Email);

            if (_user == null)
            {
                _user = new User {
                    Email = appUser.Email, UserName = appUser.Email
                };
                var _reult = await _unitOfWork.ApplicationUserManager.CreateAsync(_user, appUser.Password);

                if (_reult.Errors.Count() > 0)
                {
                    return(new OperationDetails(false, _reult.Errors.FirstOrDefault(), ""));
                }
                // добавляем роль
                await _unitOfWork.ApplicationUserManager.AddToRoleAsync(_user.Id, appUser.Role);

                // создаем профиль клиента
                UserProfile _userProfile = new UserProfile {
                    Id = _user.Id, Address = appUser.Address, Name = appUser.Name
                };
                _unitOfWork.ApplicationUserProfileManager.Create(_userProfile);
                await _unitOfWork.SaveAsync();

                return(new OperationDetails(true, "Регистрация успешно пройдена", ""));
            }
            else
            {
                return(new OperationDetails(false, "Пользователь с таким логином уже существует", "Email"));
            }
        }