Beispiel #1
0
        public async Task <IActionResult> Register([FromBody] RegistrationViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                var userIdentity = _mapper.Map <ApplicationUser>(model);
                var result       = await _userManager.CreateAsync(userIdentity, model.Password);

                if (!result.Succeeded)
                {
                    return(new BadRequestObjectResult(Errors.AddErrorsToModelState(result, ModelState)));
                }
                var code = await _userManager.GenerateEmailConfirmationTokenAsync(userIdentity);

                var callbackUrl = Url.Action($"ConfirmEmail", $"Account", new { userId = userIdentity.Id, code = code }, protocol: HttpContext.Request.Scheme);
                await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
                                                  _emailBuilder.CreateConfirmEmailBody(callbackUrl));

                //await _signInManager.SignInAsync(user, isPersistent: false);
                //await _customerService.CreateAsync(new Customer {IdentityId = userIdentity.Id});
                //await _appDbContext.Customers.AddAsync(new Customer { IdentityId = userIdentity.Id});
                await _userManager.AddToRolesAsync(userIdentity, new List <string> {
                    Constants.StandartRole, Constants.Admin
                });

                //await _appDbContext.SaveChangesAsync();
                return(new OkObjectResult("Account created"));
            }
            catch (Exception e)
            {
                return(BadRequest(new { e.Message }));
            }
        }