public async Task <IActionResult> Registerfor_jsk(RegisterModel_s registerModel_s) { if (ModelState.IsValid) { AppUser appUser = await _userManager.FindByEmailAsync(registerModel_s.Email); if (appUser != null) { ModelState.AddModelError("", "This user already exists"); } else { appUser = registerModel_s; #region User and it's role creation witrh transaction if (await this.RegisterUserAsync(registerModel_s, appUser)) { return(RedirectToAction(nameof(HomeController.Index), "Home")); } #endregion } return(View()); } return(View()); }
private async Task <bool> RegisterUserAsync(RegisterModel_s registerModel_s, AppUser appUser) { bool isRegistered = false; using (IDbContextTransaction dbContextTransaction = _jobstoreDbContext.Database.BeginTransaction()) { try { IdentityResult identityResult = await _userManager.CreateAsync(appUser, registerModel_s.Password); if (identityResult.Succeeded) { IdentityResult identityRoleResult = await _userManager.AddToRoleAsync(appUser, RoleType.sUser.ToString()); if (identityRoleResult.Succeeded) { dbContextTransaction.Commit(); isRegistered = true; } else { dbContextTransaction.Rollback(); this.AddToModelErrors(identityResult.Errors); } } else { this.AddToModelErrors(identityResult.Errors); dbContextTransaction.Rollback(); } } catch (Exception exp) { dbContextTransaction.Rollback(); ModelState.AddModelError("", exp.Message); } } return(isRegistered); }
public async Task <IActionResult> Login(Models.ViewModels.LoginModel loginModel) { if (ModelState.IsValid) { //get current user by email AppUser currentUser = await _userManager.FindByEmailAsync(loginModel.Email); Microsoft.AspNetCore.Identity.SignInResult signInResult = await _signInManager.PasswordSignInAsync(currentUser, loginModel.Password, loginModel.RememberMe, true); if (signInResult.Succeeded) { var user = User; AdminRegisterModel adminRegisterModel = new AdminRegisterModel(); RegisterModel_a registerModel_A = new RegisterModel_a(); RegisterModel_s registerModel_S = new RegisterModel_s(); if (currentUser.Email == adminRegisterModel.Email) { return(RedirectToAction("AdminIndex", "AdminHome", new { area = "Admin" })); } else if (currentUser.Email == registerModel_A.Email || currentUser.Email == registerModel_S.Email) { return(RedirectToAction("Index", "Home")); } //go to user defined role panel } else if (signInResult.IsLockedOut) { ModelState.AddModelError("", "This use is already locked out!!"); } else if (signInResult.IsNotAllowed) { ModelState.AddModelError("", "Your have no access"); } } return(RedirectToAction(nameof(Login))); }