Reputation: 11
I'm trying to get used to ASP.NET Core MVC, and I'm having trouble with user roles.
After I successfully registered with the admin role account, but when I debug, the following line of code returns null
var userRoles = await userManager.GetRolesAsync(user); // returns null
and condition always returns false:
<ul class="nav navbar-nav">
<li><a href="/Dashboard/Display">Dashboard</a></li>
@if (User.IsInRole("admin"))
{
<li><a href="/Admin/Display">Admin Page</a></li>
}
<li><a href="/UserAuthentication/Logout">Log Out</a></li>
</ul>
I don't know where I'm going wrong, Can anyone help me?
Source for it: https://github.com/tuantv3012/HotelManagement
Upvotes: 1
Views: 115
Reputation: 22082
The root cause for this issue is you are missing insert data into dbo.AspNetUserRoles table.
You can copy the userid from db.AspNetUsers table and copy the roleid from dbo.AspNetRoles table for the specific test user. The login agin for testing.
If it works, please try to add more logic about map useid and roleid in ResgistrationAsync
method.
Test Result
Upvotes: 0