Example #1
0
        public async Task<ActionResult> Login(LoginViewModel LoginViewModel, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                var originalModel = new LoginRegistrationVM();
                originalModel.LoginViewModel = LoginViewModel;
                return View("~/View/Home/Index.cshtml", originalModel);
            }

            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, change to shouldLockout: true
            var result = await SignInManager.PasswordSignInAsync(LoginViewModel.Email, LoginViewModel.Password, LoginViewModel.RememberMe, shouldLockout: false);
            switch (result)
            {
                case SignInStatus.Success:
                    var aspNetUser = UserManager.FindByName(LoginViewModel.Email);
                    if (UserManager.IsInRole(aspNetUser.Id, "Admin"))
                    {
                        return RedirectToAction("Index", "Admin");
                    }
                    if (UserManager.IsInRole(aspNetUser.Id, "Teacher"))
                    {
                        return RedirectToAction("Index", "Teacher");
                    }
                    if (UserManager.IsInRole(aspNetUser.Id, "Student"))
                    {
                        return RedirectToAction("Index", "Student");
                    }
                    if (UserManager.IsInRole(aspNetUser.Id, "Parent"))
                    {
                        return RedirectToAction("Index", "Parent");
                    }

                    return RedirectToAction("NotApprovedYet", "Home");
                    
                case SignInStatus.LockedOut:
                    return View("Lockout");
                case SignInStatus.RequiresVerification:
                    return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = LoginViewModel.RememberMe });
                case SignInStatus.Failure:
                default:
                    ModelState.AddModelError("", "Invalid login attempt.");
                    var originalModel = new LoginRegistrationVM();
                    originalModel.LoginViewModel = LoginViewModel;
                    return View("~/View/Home/Index.cshtml", originalModel);
            }
        }
        public async Task<ActionResult> Login(LoginViewModel LoginViewModel, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                var gradeLevelResponse = _lmsUserManager.GetAllGradeLevels();
                if (gradeLevelResponse.Success)
                {
                    var vm = new LoginRegistrationVM();
                    vm.LoginViewModel = LoginViewModel;
                    vm.RegisterViewModel.PopulateLists(gradeLevelResponse.Data);
                    return View("~/Views/Home/Index.cshtml", vm);
                }
                else
                {
                    ViewBag.ErrorMsg = gradeLevelResponse.Message;
                    return View("Error");
                }
            }

            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, change to shouldLockout: true
            var result = await SignInManager.PasswordSignInAsync(LoginViewModel.Email, LoginViewModel.Password, LoginViewModel.RememberMe, shouldLockout: false);
            switch (result)
            {
                case SignInStatus.Success:
                    var aspNetUser = UserManager.FindByName(LoginViewModel.Email);
                    var roles = _userManager.GetRoles(aspNetUser.Id);
                    if (!roles.Any())
                    {
                        return RedirectToAction("NotApprovedYet", "Home");
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }

                    //if (UserManager.IsInRole(aspNetUser.Id, "Admin"))
                    //{
                    //    return RedirectToAction("Index", "Admin");
                    //}
                    //else if (UserManager.IsInRole(aspNetUser.Id, "Teacher"))
                    //{
                    //    return RedirectToAction("Index", "Teachers");
                    //}
                    //else if (UserManager.IsInRole(aspNetUser.Id, "Parent"))
                    //{
                    //    return RedirectToAction("Index", "Parents");
                    //}
                    //else if (UserManager.IsInRole(aspNetUser.Id, "Student"))
                    //{
                    //    return RedirectToAction("Index", "Students");
                    //}
                    //return RedirectToAction("NotApprovedYet", "Home");

                case SignInStatus.LockedOut:
                    return View("Lockout");
                case SignInStatus.RequiresVerification:
                    return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = LoginViewModel.RememberMe });
                case SignInStatus.Failure:
                default:
                    ModelState.AddModelError("", "Invalid login attempt.");
                    var gradeLevelResponse = _lmsUserManager.GetAllGradeLevels();
                    if (gradeLevelResponse.Success)
                    {
                        var vm = new LoginRegistrationVM();
                        vm.LoginViewModel = LoginViewModel;
                        vm.RegisterViewModel.PopulateLists(gradeLevelResponse.Data);
                        return View("~/Views/Home/Index.cshtml", vm);
                    }
                    else
                    {
                        ViewBag.ErrorMsg = gradeLevelResponse.Message;
                        return View("Error");
                    }
            }
        }
Example #3
0
 //public List<SelectListItem> GradeLevels { get; set; }
 public LoginRegistrationVM()
 {
     LoginViewModel = new LoginViewModel();
     RegisterViewModel = new RegisterViewModel();
 }