Why does @User.IsInRole always return false in _Layout.cshtml


I am using Razor-Pages to develop a web app. In my _Layout.cshtml file, I want to change the menu according to the role of the current user. I, therefore, use User.IsInRole(string role) but it always returns false.

In a similar question, I read that it's somehow not possible to retrieve the user-role right after login. However, I don't understand why that would be the case.

My code:

@if (User.IsInRole(Roles.Admin.ToString())) {
  <li><a asp-page="/AdminMenuPoint">Admin Menu</a>a/li>

My roles enum:

public enum Roles {
  Supervisor, Admin

To summarize: Why doesn't User.IsInRole() work for my hompage (after login)?

Thanks in advance.

Alexandre Rodrigues

If you use .Net Core you need to setup:

  1. Add Identity Service in Startup.cs


   .AddRoles<IdentityRole>() // <-- Add this line

According to this discussion on GitHub, getting the roles and claims to show up in the cookie involves either reverting to the service.AddIdentity initialization code, or sticking with service.AddDefaultIdentity and adding this line of code to ConfigureServices:

// Add Role claims to the User object
// See: https://github.com/aspnet/Identity/issues/1813#issuecomment-420066501
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, UserClaimsPrincipalFactory<ApplicationUser, IdentityRole>>();
  1. Create Role and Assign User for Role
private async Task CreateUserRoles(IServiceProvider serviceProvider)
 var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
 var UserManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();

 IdentityResult roleResult;
 //Adding Admin Role
 var roleCheck = await RoleManager.RoleExistsAsync("Admin");
 if (!roleCheck)
 //create the roles and seed them to the database
 roleResult = await RoleManager.CreateAsync(new IdentityRole("Admin"));
 //Assign Admin role to the main User here we have given our newly registered 
 //login id for Admin management
 ApplicationUser user = await UserManager.FindByEmailAsync("[email protected]");
 var User = new ApplicationUser();
 await UserManager.AddToRoleAsync(user, "Admin");

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at


Login to comment


User.IsInRole return false

User.IsInRole always returns false with Token Authentication

User.IsInRole() always returns false only in controller

Why does my getColor function always return false?

Why does "n&1 == 0" always return false?

Why does Thread.isInterrupted () always return false?

Why does ImageButton.onSetAlpha() return false always?

Why does this return False

Why does False==False in [False] return True?

Always return false when i authenticate an user

Does "instanceof Void" always return false?

Why does isPrototypeOf() return false?

In JQuery, why does $(this) == $(this) return false?

Why does IsGenericTypeDefinition return false?

Why does the function return false?

ASP.NET Core Identity 2: User.IsInRole always returns false

Proper way to assess Role in Authorization as User.IsInRole() always returns false

User.IsInRole always returns false in View or code using Policy based Authorization

Why this android function always return false?

Why isPasswordValid() function always return false?

Why Laravel Auth::check always return false?

Why does the -e file existence test always return false for a filename containing non-ASCII characters?

Using Google Sheets scripts, why does my if statement always return false when comparing cell values?

Why does my function always returns false, even if it finds return true before?

Why does "not(True) in [False, True]" return False?

Why @User.IsInRole not working?

Why does sonar think this expression is always false

Why does `checkInputlength` always returns false?

Why does '() is ()' return True when '[] is []' and '{} is {}' return False?