예제 #1
0
        public async Task <IActionResult> RegisterAction(RegisterModel registerModel)
        {
            if (ModelState.IsValid)
            {
                IdentityResult  result;
                UserAccount     user;
                UserAccountType accountType = registerModel.AccountType == "teacher" ? UserAccountType.TeacherAccount : UserAccountType.ParentAccount;

                var pH = new PasswordHasher <UserAccount>();

                if (accountType == UserAccountType.TeacherAccount)
                {
                    if (_schoolClasses.GetById(registerModel.TeacherSchoolClassId) == null)
                    {
                        return(RedirectToAction("Error", "Home"));
                    }

                    var newTeacher = new TeacherAccount
                    {
                        UserName = registerModel.RegisterLogin, /*ALSO KNOWN AS EMAIL */
                        Email    = registerModel.RegisterLogin,
                        Name     = registerModel.FirstName,
                        LastName = registerModel.LastName,
                    };

                    newTeacher.PasswordHash = pH.HashPassword(newTeacher, registerModel.RegisterPassword);
                    _schoolClasses.GetById(registerModel.TeacherSchoolClassId).TeacherAccount = newTeacher;
                    result = await _userManager.CreateAsync(newTeacher);

                    user = (UserAccount)newTeacher;
                }
                else
                {
                    var newParent = new ParentAccount()
                    {
                        UserName     = registerModel.RegisterLogin, /*ALSO KNOWN AS EMAIL */
                        Email        = registerModel.RegisterLogin,
                        Name         = registerModel.FirstName,
                        LastName     = registerModel.LastName,
                        PasswordHash = registerModel.RegisterPassword
                    };

                    newParent.PasswordHash = pH.HashPassword(newParent, registerModel.RegisterPassword);
                    result = await _userManager.CreateAsync(newParent);

                    user = (UserAccount)newParent;
                }

                if (result.Succeeded)
                {
                    await _signInManager.SignInAsync(user, true);

                    return(RedirectToAction("Success"));
                }
            }

            return(RedirectToAction("Error", "Home"));
        }
예제 #2
0
 public void Add(ParentAccount newParentAccount)
 {
     context.Add(newParentAccount);
     context.SaveChanges();
 }