public async Task<ActionResult> Login(LoginModel details, string returnUrl) { if (ModelState.IsValid) { AppUser user = await UserManager.FindAsync(details.UserName, details.Password); if (user == null) { ModelState.AddModelError("", "Invalid name or password."); } else { ClaimsIdentity ident = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); AuthManager.SignOut(); AuthManager.SignIn(new AuthenticationProperties { IsPersistent = false }, ident); //return Redirect(returnUrl); //role based start page //role Admin go to Admin page if (UserManager.IsInRole(user.Id, "Administrators")) { return RedirectToAction("Index", "Home", new { area = "Admin" }); } //role Teacher go to Teacher page else if (UserManager.IsInRole(user.Id, "Teacher")) { return RedirectToAction("Index", "Home", new { area = "Teachers" }); } //role Student go to Student page else if (UserManager.IsInRole(user.Id, "Student")) { return RedirectToAction("Index", "Home", new { area = "Students" }); } else { //no role return RedirectToAction("Login", "Account", new { area = "" }); } } } ViewBag.returnUrl = returnUrl; return View(details); }
public async Task<ActionResult> Login(LoginModel details, string returnUrl) { if (ModelState.IsValid) { AppUser user = await UserManager.FindAsync(details.UserName, details.Password); if (user == null) { ModelState.AddModelError("", "Invalid name or password."); } else { ClaimsIdentity ident = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); AuthManager.SignOut(); AuthManager.SignIn(new AuthenticationProperties { IsPersistent = false }, ident); return Redirect(returnUrl); } } ViewBag.returnUrl = returnUrl; return View(details); }