public async Task <ActionResult> Add(Admin admin) { if (ModelState.IsValid) { var userRepository = new ApplicationUserRepository <Admin>(); var roleRepository = new ApplicationRoleRepository(); var role = roleRepository.GetByNameAsync("admin"); var isUserExist = userRepository.IsUserExistAsync(admin.UserName); if (await isUserExist) { TempData["error"] = "Account with this name already exist!"; return(View("Create", admin)); } Admin _admin = new Admin() { UserName = admin.UserName, Email = admin.Email, Role = await role, PasswordHash = ApplicationPasswordHasher.HashPasword(admin.PasswordHash), FirstName = admin.FirstName, LastName = admin.LastName }; new ApplicationUserRepository <Admin>().Create(_admin); TempData["success"] = "Admin account was added"; } else { return(View("Create", admin)); } return(RedirectToAction("Admins")); }
public async Task <ActionResult> RegisterTester(Tester tester) { ModelState.Remove(nameof(tester.FirstName)); ModelState.Remove(nameof(tester.LastName)); if (ModelState.IsValid) { var userRepository = new ApplicationUserRepository <ApplicationUser>(); var roleRepository = new ApplicationRoleRepository(); var role = roleRepository.GetByNameAsync("tester"); var isUserExist = userRepository.IsUserExistAsync(tester.UserName); if (await isUserExist) { TempData["error"] = "Account with this name already exist!"; return(View("Tester")); } Tester _tester = new Tester() { UserName = tester.UserName, Email = tester.Email, Role = await role, PasswordHash = ApplicationPasswordHasher.HashPasword(tester.PasswordHash) }; _tester.Address = new Address() { User = _tester }; new ApplicationUserRepository <Tester>().Create(_tester); ClaimsIdentity identity = await AppUserManager.CreateIdentityAsync(_tester, DefaultAuthenticationTypes.ApplicationCookie); identity.AddClaim(new Claim(ClaimTypes.Role, _tester.Role.Name)); AuthManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); AuthManager.SignIn(new AuthenticationProperties() { AllowRefresh = true, IsPersistent = false, ExpiresUtc = DateTime.UtcNow.AddMinutes(10) }, identity); return(RedirectToAction("Index", "Home", new { area = "Testing" })); } return(View("Tester")); }
public async Task <ActionResult> RegisterCompany(Company company) { ModelState.Remove(nameof(company.Rating)); ModelState.Remove(nameof(company.Reviews)); ModelState.Remove(nameof(company.CompanyName)); if (ModelState.IsValid) { var userRepository = new ApplicationUserRepository <ApplicationUser>(); var roleRepository = new ApplicationRoleRepository(); var role = roleRepository.GetByNameAsync("company"); var isUserExist = userRepository.IsUserExistAsync(company.UserName); if (await isUserExist) { TempData["error"] = "Account with this name already exist!"; return(View("Company")); } Company _company = new Company() { UserName = company.UserName, Email = company.Email, Role = await role, PasswordHash = ApplicationPasswordHasher.HashPasword(company.PasswordHash), Address = new Address() }; new ApplicationUserRepository <Company>().Create(_company); ClaimsIdentity identity = await AppUserManager.CreateIdentityAsync(_company, DefaultAuthenticationTypes.ApplicationCookie); AuthManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); AuthManager.SignIn(new AuthenticationProperties() { AllowRefresh = true, IsPersistent = false, ExpiresUtc = DateTime.UtcNow.AddMinutes(10) }, identity); return(RedirectToAction("Index", "Home", new { area = "Testing" })); } return(View("Company")); }