Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
After the successful login of a user, how can we display pages to the user according to his role. Suppose if his role is "admin" he will be rendered to "Welcome Admin User" or if normal user "Welcome!!!".
So far I've done till the login page, where the user is displayed single "AfterLogin" page. But do not know how to do this, show different pages to different access level user.

SQL
public class AppUserController : Controller
    {
        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Login(AppUser appUser)
        {
            if (ModelState.IsValid)
            {
                using (TestSystemEntities db = new TestSystemEntities())
                {
                    var v = db.AppUsers.Where(a => a.USERNAME.Equals(appUser.USERNAME) && a.PASSWORD.Equals(appUser.PASSWORD)).SingleOrDefault();
                    if (v != null)
                    {
                        Session["LogedUserFullname"] = v.USERNAME.ToString();
                        Session["LoggedUserRole"] = v.USERROLE.ToString();
                        return RedirectToAction("AfterLogin");
                    }
                    else ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }
            return View(appUser);
        }
        public ActionResult AfterLogin()
        {
            //if (Session["LogedUserID"] != null) { return View(); }
            //else { return RedirectToAction("Index"); }
            return View();
        }
    }


Thanks!
Posted
Comments
ramyajaya 9-Apr-15 15:43pm    
As per your loginc

You can proceed like this
//if (Session["LogedUserID"] != null) { return View("userview"); }
//else { return RedirectToAction("Index"); }
return View("indexview");
}
}


Where userview and indexview are different view which you can create.

Else you can also create partialviews and render them same way as above.

Refer http://www.asp.net/mvc/overview/getting-started/introduction/adding-a-view

For a basic understanding
ramyajaya 9-Apr-15 18:37pm    
1. You can create views or partial view for admin and users separately
2. Fetch the role for the current user from your current db or db context
3. Return the appropriate view using

return View ("viewname")

You can refer

http://www.codeproject.com/Articles/799571/ASP-NET-MVC-Extending-ASP-NET-Identity-Roles
http://www.codeproject.com/Articles/682113/Extending-Identity-Accounts-and-Implementing-Rol


http://www.codeproject.com/Articles/799571/ASP-NET-MVC-Extending-ASP-NET-Identity-Roles

1 solution

If you want to just write "if his role is 'admin' he will be rendered to "Welcome Admin User" or if normal user "Welcome!!!"." then you can use ViewBag and save the text as per the Session in Controller. Otherwise you can check for Session in the view itself also.

If you want to show different view then you can create different view as per the roles and then

C#
public ActionResult AfterLogin()       
{
    if (Session["LogedUserID"] == null) 
        { return View("Login"); }
    if (Session["LoggedUserRole"].ToString() == "admin")
        { return View("AdminDashBoard"); }
    else if (Session["LoggedUserRole"].ToString() == "employee")
        { return View("EmployeeDashBoard"); }

    //else { return RedirectToAction("Index"); }
    return View();
}


There are many ways, whichever you like, you can go through that one.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900