public AdminController( ApplicationDbContext context, UserManager <ApplicationUser> userManager, SignInManager <ApplicationUser> signInManager, IEmailSender emailSender, ILogger <AccountController> logger ) { _context = context; AdminAccountLayer.InitAdminAccountLayer(context); _userManager = userManager; _signInManager = signInManager; _logger = logger; }
public async Task <IActionResult> RegisterAdmin(AdminRegisterViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (!ModelState.IsValid) { return(RedirectToAction(controllerName: $"Home", actionName: "About")); } var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { // Session Information HttpContext.Session.SetString(SessionUserName, user.UserName); _logger.LogInformation("New Admin Account Created!"); // Signin New User await _signInManager.SignInAsync(user, false); _logger.LogInformation("New User Signed In."); // Add to Role await _userManager.AddToRoleAsync(user, ModelRole); // If All Went Well- Go to Employee Home Page return(RedirectToLocal(returnUrl)); } if (result.Succeeded) { return(RedirectToAction(controllerName: $"Home", actionName: "About")); } model.Errors = AdminAccountLayer.FormatIdentityResultErrors(result); return(View(model)); }