public async Task <IActionResult> Register(RegisterViewModel model, IFormFile Image, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { int count; using (Stream imageStream = Image.OpenReadStream()) { count = await _validater.DetectTheFaces(imageStream); } if (count <= 0) { ModelState.AddModelError("Image", "Image does not contain a face"); return(View(model)); } if (count > 1) { ModelState.AddModelError("Image", "The image must contain only one face"); return(View(model)); } var user = new ApplicationUser { UserName = model.UserName, Email = model.Email }; using (var memoryStream = new MemoryStream()) { await model.Image.CopyToAsync(memoryStream); user.Image = memoryStream.ToArray(); } //TODO : SAVE USER / USERROLE / INITIAL TRANSACTION in the same transaction var result = await _userManager.CreateAsync(user, model.Password); await _userManager.AddToRoleAsync(user, "User"); //Add Initial Balance after Registration _applicationDbContext.GetDBSet <Transaction>().Add(new Transaction { UserId = user.Id, Amount = 10, Comment = "Initial Balance", TransactionDate = DateTime.Now }); await _applicationDbContext.SaveChangesAsync(); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme); await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl); await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation("User created a new account with password."); return(RedirectToLocal(returnUrl)); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }