public ActionResult AttemptFinalQuiz(int Id) { DB44Entities db = new DB44Entities(); StudentModels.FinalQuizModel model = new StudentModels.FinalQuizModel(); model.course = db.Courses.SingleOrDefault(i => i.Id == Id); foreach (Question q in model.course.Questions) { model.questions.Add(q); } return(View(model)); }
public List <AspNetUser> getStudents() { DB44Entities db = new DB44Entities(); List <AspNetUser> students = new List <AspNetUser>(); foreach (AspNetUser u in db.AspNetUsers.ToList()) { if (UserManager.IsInRole(u.Id, "Student")) { students.Add(u); } } return(students); }
public List <AspNetUser> getInstructors() { DB44Entities db = new DB44Entities(); List <AspNetUser> instructors = new List <AspNetUser>(); foreach (AspNetUser u in db.AspNetUsers.ToList()) { if (UserManager.IsInRole(u.Id, "Instructor")) { instructors.Add(u); } } return(instructors); }
public HomeModel() { DB44Entities db = new DB44Entities(); AspNetRole role = db.AspNetRoles.Single(x => x.Name == "Instructor"); List <AspNetUser> instructors = new List <AspNetUser>(); foreach (AspNetUser u in db.AspNetUsers.ToList()) { if (u.AspNetRoles.Contains(role)) { instructors.Add(u); } } allCourses = db.Courses.ToList(); allInstructors = instructors; }
public async Task <ActionResult> Login(LoginViewModel model, string returnUrl) { if (!ModelState.IsValid) { return(View(model)); } // 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(model.Email, model.Password, model.RememberMe, shouldLockout : false); switch (result) { case SignInStatus.Success: if (returnUrl == "default") { DB44Entities db = new DB44Entities(); var user = db.AspNetUsers.Single(i => i.UserName == User.Identity.Name); if (UserManager.IsInRole(user.Id, "Admin")) { return(RedirectToAction("Index", "Admin")); } if (UserManager.IsInRole(user.Id, "Instructor")) { return(RedirectToAction("Index", "Instructor")); } if (UserManager.IsInRole(user.Id, "Student")) { return(RedirectToAction("Index", "Student")); } } return(RedirectToLocal(returnUrl)); case SignInStatus.LockedOut: return(View("Lockout")); case SignInStatus.RequiresVerification: return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe })); case SignInStatus.Failure: default: ModelState.AddModelError("", "Invalid login attempt."); return(View(model)); } }
public static async void PopulateDB() { var roleManager = new RoleManager <Microsoft.AspNet.Identity.EntityFramework.IdentityRole>(new RoleStore <IdentityRole>(new ApplicationDbContext())); //var userManager = new UserManager<Microsoft.AspNet.Identity.EntityFramework.IdentityUser>(new UserStore<ApplicationUser>(new DB44Entities())); var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext())); //ApplicationUserManager userM = new ApplicationUserManager(); if (!roleManager.RoleExists("Student")) { var role = new IdentityRole(); role.Name = "Student"; roleManager.Create(role); } if (!roleManager.RoleExists("Instructor")) { var role = new IdentityRole(); role.Name = "Instructor"; roleManager.Create(role); } if (!roleManager.RoleExists("Admin")) { var role = new IdentityRole(); role.Name = "Admin"; roleManager.Create(role); } bool adminExists = false; using (DB44Entities db = new DB44Entities()) { AspNetRole role = db.AspNetRoles.Single(x => x.Name == "Admin"); foreach (AspNetUser u in db.AspNetUsers.ToList()) { if (u.AspNetRoles.Contains(role)) { adminExists = true; break; } } } using (new ApplicationDbContext()) { if (!adminExists) { var user = new ApplicationUser { UserName = "******", Email = "*****@*****.**", FirstName = "Admin", LastName = "Admin", City = "City", Cnic = "1234567890000", Contact = "03000000000", DOB = DateTime.Now.Date, RegistrationDate = DateTime.Now.Date }; var result = await userManager.CreateAsync(user, "Admin@123"); if (result.Succeeded) { userManager.AddToRole(user.Id, "Admin"); } } } }