public async Task <IActionResult> Department(DepartmentManagment newdepartment)
        {
            if (!ModelState.IsValid)
            {
                return(View(new DepartmentManagment {
                    departments = GetDepartments()
                }));
            }
            int    id   = _dbContext.Department.Count() + 1;
            string name = newdepartment.Name.ToUpper();
            await _dbContext.Department.AddAsync(new Department { Did = id, Dname = name });

            await _dbContext.SaveChangesAsync();

            return(RedirectToAction("Department"));
        }
示例#2
0
        public async Task <IActionResult> ManualRegister(AllInfo student)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }
            else
            {
                //Remove any spaces in the first
                student.newStudnet.Id         = student.newStudnet.Id.Trim();
                student.newStudnet.Name       = student.newStudnet.Name.Trim();
                student.newStudnet.Pass       = student.newStudnet.Pass.Trim();
                student.newStudnet.RepeatPass = student.newStudnet.RepeatPass.Trim();

                //check if the target user is used before or not
                var targetUser = _dbContext.InfoTable.SingleOrDefault(i => i.Id.Equals(student.newStudnet.Id, StringComparison.CurrentCulture));
                var targetGPA  = _dbContext.InfoTable.SingleOrDefault(i => i.Id.Equals(student.newStudnet.Id, StringComparison.CurrentCulture));
                if (targetUser != null)
                {
                    ViewBag.IdTarget = "This ID is already used";
                    return(View());
                }
                //hash password
                var hasher = new PasswordHasher <InfoTable>();
                targetUser = new InfoTable {
                    Id = student.newStudnet.Id, Name = student.newStudnet.Name, RoleId = 2
                };
                targetUser.Pass = hasher.HashPassword(targetUser, student.newStudnet.Pass);

                //chech the GPA
                if (student.newStudnet.StudentGPA != double.NaN)
                {
                    await GpaSet(student.newStudnet);
                }
                await _dbContext.InfoTable.AddAsync(targetUser);

                await _dbContext.SaveChangesAsync();

                ViewBag.templist = true;
                TempRepository.AddStudent(student.newStudnet);
                ViewBag.inserted = true;
                return(View(new AllInfo
                {
                    Studentslist = TempRepository.Inserted
                }));
            }
        }
        public async Task <IActionResult> StudentSelect(StuSelect student)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("FinalSelection"));
            }
            else
            {
                var targetUser = _dbContext.StuSelection.SingleOrDefault(i => i.StuId.Equals(User.Identity.Name, StringComparison.CurrentCulture));

                targetUser = new StuSelection {
                    StuId = User.Identity.Name, FirstSelection = student.Selection.Department1, SecondSelection = student.Selection.Department2, ShirdSelection = student.Selection.Department3, FourthSelection = student.Selection.Department4
                };
                await _dbContext.StuSelection.AddAsync(targetUser);

                await _dbContext.SaveChangesAsync();

                return(RedirectToAction("StudentSelect", "StudentSelection"));
            }
        }
示例#4
0
        //[AllowAnonymous, HttpGet]
        //public async Task<IActionResult> Register()
        //{
        //    await
        //        HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
        //    return View();
        //}

        //[AllowAnonymous, HttpPost]
        //public async Task<IActionResult> Register(RegisterViewModel model)
        //{
        //    if (!ModelState.IsValid)
        //    {
        //        throw new Exception("Invalid registration information.");
        //    }

        //    model.Name = model.Name.Trim();
        //    model.Password = model.Password.Trim();
        //    model.RepeatPassword = model.RepeatPassword.Trim();
        //    model.Id = model.Id.Trim();
        //    var targetUser = _dbContext.InfoTable.SingleOrDefault(i => i.Id.Equals(model.Id, StringComparison.CurrentCulture));

        //    if (targetUser != null)
        //    {
        //        throw new Exception("User name already exists.");
        //    }

        //    if (!model.Password.Equals(model.RepeatPassword))
        //    {
        //        throw new Exception("Passwords are not identical.");
        //    }

        //    var hasher = new PasswordHasher<InfoTable>();
        //    targetUser = new InfoTable { Name = model.Name, Id = model.Id, RoleId = 1};
        //    targetUser.Pass = hasher.HashPassword(targetUser, model.Password);



        //    await _dbContext.InfoTable.AddAsync(targetUser);
        //    await _dbContext.SaveChangesAsync();
        //    await LogInUserAsync(targetUser);

        //    return RedirectToAction("Index", "Home");
        //}

        private async Task LogInUserAsync(InfoTable user)
        {
            var claims = new List <Claim>();

            claims.Add(new Claim(ClaimTypes.Name, user.Id));

            if (user.RoleId == 1)
            {
                claims.Add(new Claim(ClaimTypes.Role, "Administrator"));
            }
            else
            {
                claims.Add(new Claim(ClaimTypes.Role, "Student"));
            }


            var claimsIndentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
            var claimsPrincipal = new ClaimsPrincipal(claimsIndentity);
            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal);

            await _dbContext.SaveChangesAsync();
        }