public async Task <IActionResult> SelfRegister([FromBody] UserEditViewModel user) { try { //if (!(await _authorizationService.AuthorizeAsync(this.User, Tuple.Create(user.Roles, new string[] { }), Authorization.Policies.AssignAllowedRolesPolicy)).Succeeded) // return new ChallengeResult(); if (!string.IsNullOrEmpty(user.Tin)) { var existingInvestor = _context.Investors.FirstOrDefault(p => p.Tin == user.Tin); if (existingInvestor == null) { ModelState.AddModelError("Tin", "A record with the provided TIN does not exist on our database!"); return(BadRequest(ModelState)); } } var _emailSender = new EmailSendGrid(_config); if (ModelState.IsValid) { if (user == null) { return(BadRequest($"{nameof(user)} cannot be null")); } var appUser = Mapper.Map <ApplicationUser>(user); var result = await _accountManager.CreateUserAsync(appUser, user.Roles, user.NewPassword); if (result.Item1) { var userVM = await GetUserViewModelHelper(appUser.Id); try { var code = await _userManager.GenerateEmailConfirmationTokenAsync(appUser); var callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId = appUser.Id, token = code })); var strUser = user.FullName; var strMessage = EmailTemplates.GetConfirmationEmail(strUser, callbackUrl.ToString()); //await _emailSender.SendEmailAsync(user.Email, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); await _emailSender.SendEmailAsync(user.Email, "Confirm your account", strMessage); } catch (Exception ex) { var s = ex.Message; } return(CreatedAtAction(GetUserByIdActionName, new { id = userVM.Id }, userVM)); } AddErrors(result.Item2); } else { var message = string.Join(" | ", ModelState.Values .SelectMany(v => v.Errors) .Select(e => e.ErrorMessage)); var s = message; } return(BadRequest(ModelState)); } catch (Exception ex) { var s = ex.Message; return(null); } }