public async Task<ActionResult> Register(RegisterViewModel model) { if (!ModelState.IsValid) return View(model); var user = new ApplicationUser { UserName = model.Username, Email = model.Email }; IdentityResult result = await IdentityRepository.UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await IdentityRepository.SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link string code = await IdentityRepository.UserManager.GenerateEmailConfirmationTokenAsync(user.Id); string callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); await IdentityRepository.UserManager.SendEmailAsync(user.Id, "Confirm your account", callbackUrl); return RedirectToAction("Index", "Home"); } AddErrors(result); return View(model); }
public async Task<ActionResult> Create(RegisterViewModel registerViewModel, params string[] selectedRoles) { if (!ModelState.IsValid) return RedirectToAction("Index"); var model = new UsersHandlerCreateViewModel { RegisterViewModel = registerViewModel, SelectedRoles = await IdentityRepository.RoleManager.Roles.ToListAsync() }; var user = new ApplicationUser { UserName = registerViewModel.Username, Email = registerViewModel.Email }; IdentityResult userResult = await IdentityRepository.UserManager.CreateAsync(user, registerViewModel.Password); if (!userResult.Succeeded) { AddErrors(userResult); return View(model); } // Confirm email manualy string token = await IdentityRepository.UserManager.GenerateEmailConfirmationTokenAsync(user.Id); await IdentityRepository.UserManager.ConfirmEmailAsync(user.Id, token); //await IdentityRepository.SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); //string code = await IdentityRepository.UserManager.GenerateEmailConfirmationTokenAsync(user.Id); //string callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); //await IdentityRepository.UserManager.SendEmailAsync(user.Id, "Confirm your account", callbackUrl); if (selectedRoles != null) { IdentityResult rolesResult = await IdentityRepository.UserManager.AddToRolesAsync(user.Id, selectedRoles); if (!rolesResult.Succeeded) { ModelState.AddModelError("", rolesResult.Errors.First()); return View(model); } } return RedirectToAction("Index"); }