Пример #1
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            if (!ModelState.IsValid)
            {
                model.Companies = _companyManager.GetCompanies().Select(x => new SelectListItem
                {
                    Value = x.Id.ToString(),
                    Text  = x.Name
                }).ToList();
                return(View(model));
            }

            var newUser = new ApplicationUser
            {
                UserName  = model.Email,
                Email     = model.Email,
                FirstName = model.FirstName,
                LastName  = model.LastName,
                CompanyId = model.CompanyId
            };

            var result = await _userManager.CreateAsync(newUser, model.Password);

            if (!result.Succeeded)
            {
                foreach (var error in result.Errors)
                {
                    _logger.LogError($"Registration error: {error.Code} - {error.Description}");
                    ModelState.AddModelError(string.Empty, error.Description);
                }

                model.Companies = _companyManager.GetCompanies().Select(x => new SelectListItem
                {
                    Value = x.Id.ToString(),
                    Text  = x.Name
                }).ToList();

                return(View(model));
            }

            try
            {
                await SendActivationEmail(newUser, returnUrl);
            } catch (Exception exp)
            {
                _logger.LogError(2, exp, "SMTP sender failed.");
            }

            return(RedirectToAction(nameof(RegistrationComplete), new
            {
                protectedSequence = _urlEncoderWrapper.UrlEncode(_protector.Protect(model.Email))
            }));
        }
Пример #2
0
        public async Task <IActionResult> Register([FromBody] UserModel userModel)
        {
            if (string.IsNullOrEmpty(userModel.UserName) || string.IsNullOrEmpty(userModel.Password))
            {
                return(BadRequest());
            }

            User user;

            user = await _userManager.FindByNameAsync(userModel.UserName);

            if (user != null)
            {
                return(BadRequest());
            }
            user = new User
            {
                UserName = userModel.UserName
            };
            var userCreation = await _userManager.CreateAsync(user, userModel.Password);

            if (userCreation.Succeeded)
            {
                var jwtToken = GenerateJwtToken(user);
                return(Ok(new { token = jwtToken }));
            }
            return(BadRequest());
        }
Пример #3
0
        public async Task <string> RegisterAsync(RegisterUserCommand command)
        {
            var user = new ApplicationUser
            {
                UserName       = command.Email,
                Email          = command.Email,
                EmailConfirmed = true
            };
            var userWithSameEmail = await _userManagerWrapper.FindByEmailAsync(command.Email);

            if (userWithSameEmail != null)
            {
                return($"Email {user.Email} is already registered.");
            }

            var result = await _userManagerWrapper.CreateAsync(user, command.Password);

            if (!result.Succeeded)
            {
                return(string.Join(" ", result.Errors.Select(x => x.Description)));
            }

            await _userManagerWrapper.AddToRoleAsync(user, Authorization.DEFAULT_ROLE.ToString());

            return($"User Registered with username {user.UserName}");
        }
Пример #4
0
        public async Task <IdentityResult> AddUser(UserForRegister user)
        {
            using (var transaction = context.Database.BeginTransaction())
            {
                var userEntity = mapper.Map <User>(user);
                var result     = await userManager.CreateAsync(userEntity, user.Password);

                if (result.Succeeded)
                {
                    result = await userManager.AddToRoleAsync(userEntity, Roles.Customer);
                }

                if (result.Succeeded)
                {
                    transaction.Commit();
                }

                return(result);
            }
        }
Пример #5
0
        public async Task <CustomeIdentityResult> SignUpUserAsync(SignInViewModel model)
        {
            var employer     = _mapper.Map <Employer>(model);
            var createResult = await _userManager.CreateAsync(employer, model.ConfirmPassword);

            var token = "";

            if (createResult.Succeeded)
            {
                await _signInManager.SignInAsync(employer, true);

                token = _tokenHandler.GenerateToken(employer);
            }
            return(new CustomeIdentityResult()
            {
                Errors = createResult.Errors.ToList(),
                Success = createResult.Succeeded,
                Token = token
            });
        }