public async Task <IActionResult> Register(CompanyViewModel companyViewModel) { if (ModelState.IsValid) { var user = new ApplicationUser() { Name = companyViewModel.Name, UserName = companyViewModel.Email, Email = companyViewModel.Email, NormalizedEmail = companyViewModel.NormalizedEmail, NormalizedUserName = companyViewModel.NormalizedUserName, ConcurrencyStamp = companyViewModel.ConcurrencyStamp, Id = companyViewModel.Id, PasswordHash = companyViewModel.PasswordHash }; _iCompanyAppService.Insert(companyViewModel); var result = await _userManager.CreateAsync(user, companyViewModel.PasswordHash); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); if (result.Succeeded) { IdentityResult roles = await _userManager.AddToRoleAsync(user, "Company"); IdentityResult claimEmail = await _userManager.AddClaimAsync(user, new Claim(ClaimTypes.Email, companyViewModel.Email)); IdentityResult claimRole = await _userManager.AddClaimAsync(user, new Claim(ClaimTypes.Role, "Company")); var claimId = await _userManager.AddClaimAsync(user, new Claim(ClaimTypes.NameIdentifier, user.Id.ToString())); if (roles.Succeeded && claimId.Succeeded && claimEmail.Succeeded && claimRole.Succeeded) { return(RedirectToAction("Login", "Account")); } } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(View(companyViewModel)); }
public IActionResult Update(CompanyViewModel companyViewModel) { var user = _userManager.GetUserAsync(User); if (ModelState.IsValid) { _iCompanyAppService.Insert(companyViewModel); if (isValided()) { ViewBag.Notifications = _notification.Get(); return(View(companyViewModel)); } return(RedirectToAction("Index", "CompanyEmployee", new { id = user.Result.Id })); } return(View(companyViewModel)); }