public async Task <IActionResult> Register(RegisterViewModel model) { if (!ModelState.IsValid) { return(View("Error")); } var user = new ConfArchUser { UserName = model.Email, Email = model.Email, BirthDate = model.BirthDate }; var result = await _userManager.CreateAsync( user, model.Password); if (!await _roleManager.RoleExistsAsync("Organizer")) { await _roleManager.CreateAsync(new IdentityRole { Name = "Organizer" }); } if (!await _roleManager.RoleExistsAsync("Speaker")) { await _roleManager.CreateAsync(new IdentityRole { Name = "Speaker" }); } if (result.Succeeded) { // Full article how to do this: // https://docs.microsoft.com/en-us/aspnet/identity/overview/features-api/account-confirmation-and-password-recovery-with-aspnet-identity //var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); //var callbackUrl = Url.Action( // "ConfirmEmail", "Account", // new { userId = user.Id, code = code }, // protocol: Request.Url.Scheme); //await UserManager.SendEmailAsync(user.Id, // "Confirm your account", // "Please confirm your account by clicking this link: <a href=\"" // + callbackUrl + "\">link</a>"); //// ViewBag.Link = callbackUrl; // Used only for initial demo. //return View("DisplayEmail"); await _userManager.AddToRoleAsync(user, model.Role); await _userManager.AddClaimAsync(user, new Claim("technology", model.Technology)); return(View("RegistrationConfirmation")); } foreach (var error in result.Errors) { ModelState.AddModelError("error", error.Description); } return(View(model)); }
public async Task <IActionResult> Register(AccountRegisterViewModel account) { if (!ModelState.IsValid) { return(View("Error")); } var user = new ConfArchUser() { UserName = account.Email, Email = account.Email, BirthDate = account.BirthDate }; var result = await _userManager.CreateAsync( user, account.Password); if (!await _roleManager.RoleExistsAsync("Organizer")) { await _roleManager.CreateAsync(new IdentityRole { Name = "Organizer" }); } if (!await _roleManager.RoleExistsAsync("Speaker")) { await _roleManager.CreateAsync(new IdentityRole { Name = "Speaker" }); } await _userManager.AddToRoleAsync(user, account.Role); await _userManager.AddClaimAsync(user, new Claim("technology", account.Technology)); if (result.Succeeded) { return(View("RegistrationConfirmation")); } foreach (var error in result.Errors) { ModelState.AddModelError("error", error.Description); } return(View(account)); }