public async Task <ActionResult> Login(LoginModel model) { if (ModelState.IsValid) { EnrolleeDTO enrolleeDto = new EnrolleeDTO { UserName = model.Email, Password = model.Password }; ClaimsIdentity claim = await EnrolleeService.Authenticate(enrolleeDto); if (claim == null) { ModelState.AddModelError("", "Неверный логин или пароль."); } else { AuthenticationManager.SignOut(); AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = true }, claim); if (User.IsInRole("admin")) { return(RedirectToAction("Faculties", "Admin")); } return(RedirectToAction("Home", "SelectionCommittee")); } } return(View(model)); }
public async Task <ClaimsIdentity> Authenticate(EnrolleeDTO enrollee) { ClaimsIdentity claim = null; ApplicationUser user = await _database.UserManager.FindAsync(enrollee.UserName, enrollee.Password); if (user != null) { claim = await _database.UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); } return(claim); }
public OperationDetails Update(EnrolleeDTO enrollee) { Enrollee enrol = new Enrollee { Id = enrollee.Id, Name = enrollee.Name, Surname = enrollee.Surname, Patronymic = enrollee.Patronymic, Photo = enrollee.Photo, CityId = enrollee.CityId, RegionId = enrollee.RegionId, EducationalInstitutionId = enrollee.EducationalInstitutionId }; _database.EnrolleeManager.Update(enrol); return(new OperationDetails(true, "Данные успешно обновлены", "")); }
public async Task <OperationDetails> Create(EnrolleeDTO enrollee) { var role = await _database.RoleManager.FindByNameAsync(enrollee.Role); if (role == null) { role = new ApplicationRole { Name = enrollee.Role }; await _database.RoleManager.CreateAsync(role); } ApplicationUser user = await _database.UserManager.FindByEmailAsync(enrollee.Email); if (user == null) { user = new ApplicationUser { Email = enrollee.Email, UserName = enrollee.UserName }; var result = await _database.UserManager.CreateAsync(user, enrollee.Password); if (result.Errors.Any()) { return(new OperationDetails(false, result.Errors.FirstOrDefault(), "")); } await _database.UserManager.AddToRoleAsync(user.Id, enrollee.Role); Enrollee enrol = new Enrollee { Id = user.Id, Name = enrollee.Name, Surname = enrollee.Surname, Patronymic = enrollee.Patronymic, CityId = enrollee.CityId, RegionId = enrollee.RegionId, EducationalInstitutionId = enrollee.EducationalInstitutionId }; _database.EnrolleeManager.Create(enrol); await _database.SaveAsync(); return(new OperationDetails(true, "Регистрация успешно пройдена", "")); } else { return(new OperationDetails(false, "Пользователь с таким логином уже существует", "Email")); } }
public async Task <ActionResult> Register(RegisterModel model) { if (ModelState.IsValid) { EnrolleeDTO enrolleeDto = new EnrolleeDTO { Email = model.Email, Password = model.Password, Name = model.Name, Surname = model.Surname, Patronymic = model.Patronymic, CityId = model.CityId, RegionId = model.RegionId, EducationalInstitutionId = model.EducationalInstitutionId, Role = "enrollee", UserName = model.Email }; OperationDetails operationDetails = await EnrolleeService.Create(enrolleeDto); if (operationDetails.Succedeed) { return(View("Login", new LoginModel { Email = model.Email, Password = model.Password })); } else { ModelState.AddModelError(operationDetails.Property, operationDetails.Message); } } model.Cities = necessaryModel.Cities; model.Regions = necessaryModel.Regions; model.EducationalInstitutions = necessaryModel.EducationalInstitutions; return(View(model)); }