Reputation: 75
I'm new to asp.net mvc 5. I have 3 user roles (Admin, Teacher, Student), all 3 roles have different view. I want to make when user is logging in, to redirect or to route user based on it's role and if is user logged in to skip login page.
Thanks in advice
I will try to explain deeper:
I have table in SQL, Users(username, password, email, role, datejoined, indexNo), and I want to when someone's try to log in if user exist in that table to check role.
if users has role admin to redirect to admin view, else if user has teacher role to redirect to teacher view, else redirect to student. All 3 view's are different look and feel with different options. When I put [Authorised] annotation, every user of application can see options of all other users, but can't access, and taht I don't want. I want to every role has it's own view with options for that role, and to not see options from another roles.
Upvotes: 3
Views: 122
Reputation: 2464
To redirect to specific view base on role, you can follow below code.
public ActionResult RedirectLogin(string returnUrl)
{
if(User.IsInRole("Admin")){
return RedirectToAction("Index", "Admin");
}
if(User.IsInRole("Teacher")){
return RedirectToAction("Index", "Teacher");
}
if(User.IsInRole("Student")){
return RedirectToAction("Index", "Student");
}
}
Upvotes: 1