Search code examples
c#asp.netasp.net-mvc-5.net-4.5asp.net-identity-2

.Net MVC 5 nested isinrole


Is it possible with ASP.NET C# MVC 5 (.NET 4.5.1) using Identity 2.0 to have nested IsInRole(). I am doing this because I want to hide a menu element to only people in the Admin role and on top of that, I want to hide a link to only people in the Developer role. That way, only developers can add roles.

For example:

@if(Request.IsAuthenticated && User.IsInRole("Admin"))
{
  <li>
      <div class="dropdown">
          <div class="dropdown-toggle" id="adminMenu" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
              Admin
              <span class="caret"></span>
          </div>
          <ul class="dropdown-menu" aria-labelledby="userMenu">
              @if(User.IsInRole("Developer"))
              {
                  <li>@Html.ActionLink("RolesAdmin", "Index", "RolesAdmin")</li>
              }
              <li>@Html.ActionLink("UsersAdmin", "Index", "UsersAdmin")</li>
              <li>@Html.ActionLink("GroupsAdmin", "Index", "GroupsAdmin")</li>
          </ul>
      </div>
  </li>
}

Any Ideas? Or am I approaching this wrong?

Thank you


Solution

  • The code always worked, I had to log out and log back in to see the updated changes with the permissions.

    I was testing this on a single account so the cookie was not getting update with the new data.

    Sorry for the trouble and thanks for the responses.