Waller
Waller

Reputation: 476

The User.IsInRole working for Admin but false for other users

In my _Layout.chtml, I want to show menu on the navbar based on the user role. To make it short, I have "Admin" and "User" role. Here is my code in the _Layout.html

    <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li>@Html.ActionLink("Home", "Index", "Home")</li>

                @if (User.IsInRole("Admin"))
        {
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Manage Users</a>
                        <ul class="dropdown-menu">
                            <li>@Html.ActionLink("User", "Index", "User")</li>
                        </ul>
                    </li>
        }
        else if (User.IsInRole("User"))
                    {
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">User</a>
                        <ul class="dropdown-menu">
                            <li>@Html.ActionLink("User2", "Index2", "User2")</li>
                        </ul>
                    </li>
        }
                else
                {
                }

            </ul>
        </div>

The problem is, When I login a user with "Admin" role, I can see the menu. But when I login using "User", nothing. As if the (User.IsInRole("User")) is false. User authenticated successfully (There's no "Wrong password" warning). What can be the cause of this?

UPDATE: Here's what I tried so far. 1. Change the user (with "User" role) role to "Admin", user can see the menu. 2. Change the arrangement of the code (If "User" ..... Else "Admin"), still "User") user cannot see the menu.

When I login with "Admin" user, it will redirect to the homepage and menu will appear. When I login using "User" user, it will stay on the login page, username textbox intact, password textbox blank, and no warning whatsoever

When I login with "Admin" user, "User.Identity.IsAuthenticated" is true. With "User", its False.

In all controller, I've added this... [Authorize(Roles = "Admin,User")]

...All to no avail.

Upvotes: 0

Views: 1193

Answers (2)

Waller
Waller

Reputation: 476

My bad. The login method in AccountController was changed and I forgot about it.

if ((UserManager.IsInRole(user.Id, "Admin")) || (UserManager.IsInRole(user.Id, "User")))
{
await SignInAsync(user, model.RememberMe);
return RedirectToLocal(returnUrl);
}

Upvotes: 0

Prasad Raja
Prasad Raja

Reputation: 703

When user is logged in successfully,

First check what are the available roles for the Authenticated user... According to this we can check in different angles

  @if (Request.IsAuthenticated)
            {
                 string[] r = Roles.GetRolesForUser();
                 string s = string.Join(",", r.ToList());
                <h1>@s</h1>
}

Upvotes: 0

Related Questions