public async Task <IActionResult> AdminRegister(RegisterViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.UserName, Email = model.Email, UserType = model.UserType }; var result = await _userManager.CreateAsync(user, model.Password); 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 _userManager.AddToRoleAsync(user, "Admin"); User_Profile_Controller user_Profile_Controller = new User_Profile_Controller { Id = user.Id, Profile_Activated = "0", Account_Activated = "1", User_Role = user.UserType }; _db.User_Profile_Controller.Add(user_Profile_Controller); Admin_Profile admin_Profile = new Admin_Profile { Id = user.Id, CreatedBy = _db.Admin_Profile.Find(_userManager.GetUserId(User)).FirstName, Email = "" }; _db.Admin_Profile.Add(admin_Profile); _db.SaveChanges(); return(RedirectToAction("Index", "Admin")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View("~/Views/Home/Register.cshtml", model)); // return View(model); }
public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.UserName, Email = model.Email, UserType = model.UserType }; var result = await _userManager.CreateAsync(user, model.Password); 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); //How to add a new role to the role table //string[] roles = { "Admin", "Lawyers", "Clients", "NotloggedIn" }; //foreach (var role in roles) //{ // if (!await _roleManager.RoleExistsAsync(role)) // { // var _role = new IdentityRole(role); // await _roleManager.CreateAsync(_role); // } //} await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation("User created a new account with password."); if (model.UserType == "Lawyer") { await _userManager.AddToRoleAsync(user, "Lawyers"); User_Profile_Controller user_Profile_Controller = new User_Profile_Controller { Id = user.Id, Profile_Activated = "0", Account_Activated = "1", User_Role = user.UserType }; _db.User_Profile_Controller.Add(user_Profile_Controller); _db.SaveChanges(); return(RedirectToAction("CreateProfile", "Lawyer")); } else if (model.UserType == "Client") { await _userManager.AddToRoleAsync(user, "Clients"); User_Profile_Controller user_Profile_Controller = new User_Profile_Controller { Id = user.Id, Profile_Activated = "2", Account_Activated = "1", User_Role = user.UserType }; _db.User_Profile_Controller.Add(user_Profile_Controller); _db.SaveChanges(); return(RedirectToAction("CreateProfile", "Client")); } else { await _userManager.AddToRoleAsync(user, "Admin"); User_Profile_Controller user_Profile_Controller = new User_Profile_Controller { Id = user.Id, Profile_Activated = "0", Account_Activated = "1", User_Role = user.UserType }; _db.User_Profile_Controller.Add(user_Profile_Controller); _db.SaveChanges(); return(RedirectToAction("CreateProfile", "Admin")); } } AddErrors(result); } // If we got this far, something failed, redisplay form return(View("~/Views/Home/Register.cshtml", model)); // return View(model); }