Пример #1
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
            });
        }
Пример #2
0
        public async Task <IActionResult> RegisterExternal(RegisterViewModel model, [FromQuery] string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                model.Companies = _companyManager.GetCompanies().Select(x => new SelectListItem
                {
                    Value = x.Id.ToString(),
                    Text  = x.Name
                }).ToList();
                return(View(model));
            }

            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                return(View("ExternalLoginFailure"));
            }

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

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

            if (result.Succeeded)
            {
                result = await _userManager.AddLoginAsync(newUser, info);

                if (result.Succeeded)
                {
                    await _signInManager.SignInAsync(newUser, isPersistent : false);

                    _logger.LogInformation(6, "User created an account using {Name} provider.", info.LoginProvider);

                    // Update any authentication tokens as well
                    await _signInManager.UpdateExternalAuthenticationTokensAsync(info);

                    _companyManager.SetAdmin(Guid.Parse(newUser.Id), newUser.CompanyId);

                    if (!string.IsNullOrWhiteSpace(returnUrl))
                    {
                        return(Redirect(returnUrl));
                    }

                    return(RedirectToAction("Index", "Home"));
                }
            }

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

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

            return(View(nameof(SignInExternal), model));
        }