Beispiel #1
0
		public async Task<ActionResult> Create(AccountViewModel model)
		{
			if (ModelState.IsValid)
			{
				ApplicationUser user = new ApplicationUser() {
					Email = model.Email,
					UserName = model.UserName,
					LockoutEnabled = true
				};

				using (var manager = UserManager)
				{
					var result = await manager.PasswordValidator.ValidateAsync(model.Password);

					if (!result.Succeeded)
					{
						foreach (var error in result.Errors)
							ModelState.AddModelError("Password", error);

						InitTeachersItems(model.PairTeacherId);

						return View("CreateAccount", model);
					}

					result = await manager.CreateAsync(user, model.Password);

					if (!result.Succeeded)
					{
						foreach (var error in result.Errors)
							ModelState.AddModelError("", error);

						InitTeachersItems(model.PairTeacherId);

						return View("CreateAccount", model);
					}

					if (model.PairTeacherId.HasValue)
						_teacherRepository.AddPairToUser(model.PairTeacherId.Value, user.Id);

					(await manager.AddToRolesAsync(user.Id, model.SelectedRoles.ToArray()))
						.AddErrorsIfFailed(TempData);

					TempData.AddMessage(MessageType.Success, string.Format(GlobalRes.UserCreatedMsg, user.UserName));
				}

				_logger.Info("User \"{0}\" created by {1}.", user.UserName, User.Identity.Name);

				return RedirectToAction(nameof(Table));
			}

			InitTeachersItems(model.PairTeacherId);
			// there is something wrong with the data values         
			return View("CreateAccount", model);
		}
Beispiel #2
0
		public ActionResult Create()
		{
			var model = new AccountViewModel();
			using (var manager = RoleManager)
				model.Roles = manager.Roles.Select(r => new RoleItem() { Value = r.Name })
										   .OrderBy(r => r.Value)
										   .AsNoTracking()
										   .ToList();

			InitTeachersItems();

			return View("CreateAccount", model);
		}