Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Check Roles for Users in Controller Actions or Methods #1001


User avatar
0
aifazk created

Hello, I'm stuck in Checking role of users in Controller action(HomeController, Index action), below described my use case, I have tried as per documentation role management and user management on ASP.NET Boilerplate site but nothing works for me

I have to navigate users to their respective layout pages on the basis of their roles, here is my code as of now but I have to replace it with users role

// Home Comtroller public ActionResult Index() {

if (HttpContext.User.Identity.Name.Equals("admin")) {

            return View("~/App/Main/views/admin/adminlayout.cshtml");
        }
        else if (HttpContext.User.Identity.Name.Equals("student"))
        {
           return View("~/App/Main/views/student/studentlayout.cshtml");
            
        }

else{ //... }

}

urgent help would be appreciated, also I would like know how could we use external webservice call inside AngularJS or Boilerplate site.

Thanks Aifaz


3 Answer(s)
  • User Avatar
    0
    winson created

    I think you can read my reply in this, maybe can help you ?

    #922

  • User Avatar
    0
    aifazk created

    Below code workes for me and done like this way, Can you Please check if it is fine to deploy, Pls Suggest

    [UnitOfWork] public ActionResult Index() { var adminRoleForDefaultTenant = new UserRole(); var role = new Role(); var UserId = _userManager.AbpSession.UserId; using (var unitOfWork = _unitOfWorkManager.Begin()) {

                adminRoleForDefaultTenant = _context.DbContext.UserRoles.FirstOrDefault(r => r.UserId == UserId);
                var roleID = adminRoleForDefaultTenant.RoleId;
                role = _context.DbContext.Roles.FirstOrDefault(r => r.Id == roleID);
               
                unitOfWork.Complete();
            }
            ////Layout of the angular application.
             if(role.Name.Equals("Admin"))
             {
                 return View("~/App/Main/views/admin/adminlayout.cshtml");
             }
             else if (role.Name.Equals("Student"))
             {
                 return View("~/App/Main/views/student/studentlayout.cshtml");
             }
    

    else{//..} }

  • User Avatar
    0
    hikalkan created
    Support Team

    Your code may work but not a good code. Also, it has a bug. It can be one-level fixed as shown below:

    [UnitOfWork]
    public virtual ActionResult Index()
    {
        var adminRoleForDefaultTenant = _context.DbContext.UserRoles.FirstOrDefault(r => r.UserId == AbpSession.UserId);
        var roleID = adminRoleForDefaultTenant.RoleId;
        var role = _context.DbContext.Roles.FirstOrDefault(r => r.Id == roleID);
    
        ////Layout of the angular application.
        if(role.Name.Equals("Admin"))
        {
        return View("~/App/Main/views/admin/adminlayout.cshtml");
        }
        else if (role.Name.Equals("Student"))
        {
        return View("~/App/Main/views/student/studentlayout.cshtml");
        }
        else{//..}
    }
    

    You missed to add virtual to method definition. So, your UOW did not work and you manually Begin a UOW. Also, I cleared your code a bit by removing unnecessary assignments.

    But... this is still not a good code. Why don't you just use user manager to check it?

    public ActionResult Index()
    {
        if (_userManager.IsInRole(AbpSession.GetUserId(), "Admin"))
        {
            return View("~/App/Main/views/admin/adminlayout.cshtml");
        }
        else if (_userManager.IsInRole(AbpSession.GetUserId(), "Student"))
        {
            return View("~/App/Main/views/student/studentlayout.cshtml");
        }
        else
        {
            //..
        }
    }