2

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.

Jimmy
  • 864
  • 13
  • 24
  • have you got `enabled="true"` for `roleManager` in the web.config? – Leo May 15 '19 at 14:18
  • 1
    https://stackoverflow.com/questions/15567338/user-isinrole-doesnt-work – st_stefanov May 15 '19 at 14:20
  • Have you wired up ASP Identity to use your `Roles` enum? There should be something like `services.AddIdentity()` in the `ConfigureServices` method of your Startup file. – Patrick Stephansen May 15 '19 at 14:26
  • @LeonardoSeccia I just did. It still doesn't work tho. – Jimmy May 15 '19 at 14:33
  • as in comment by @st_stefanov try: ` ` – Leo May 15 '19 at 14:38
  • @PatrickStephansen In `ConfigureServices` I have both `services.AddDefaultIdentity()` and `services.AddRoles()`. For testing purposes, I seed users with roles as follows: `roleManager.CreateAsync(new IdentityRole(Roles.Admin.ToString()))` – Jimmy May 15 '19 at 14:40
  • outside `` inside `` - I have added it as an answer as it is too difficult to show you in a comment... if it does not work i will delete it. – Leo May 15 '19 at 14:54
  • @LeonardoSeccia It still doesn't work :( – Jimmy May 15 '19 at 15:02
  • have you try to add the user to a role, like calling `await userManager.AddToRoleAsync(User, Roles.Admin.ToString())` and check the database to ensure that the setting persisted? – Riza May 15 '19 at 16:04
  • @riza Yes, that's exactly what I did. I've also checked the database and both the roles and the connection between role and user is as I expected... – Jimmy May 16 '19 at 06:37

1 Answers1

2

If you use .Net Core you need to setup:

  1. Add Identity Service in Startup.cs

Edited

services.AddDefaultIdentity<ApplicationUser>()
   .AddRoles<IdentityRole>() // <-- Add this line
    .AddEntityFrameworkStores<ApplicationDbContext>();

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("syedshanumcain@gmail.com");
 var User = new ApplicationUser();
 await UserManager.AddToRoleAsync(user, "Admin");
}

enter image description here

  • Thanks, but that's not my problem. The seeding of users and their roles seems not to be the problem – Jimmy May 15 '19 at 15:03
  • @Jimmy are the user assigned to roles on table ASPNetUserRoles? – Alexandre Rodrigues May 15 '19 at 15:09
  • Thank you for your help. I just checked `AspNetUserRoles` in my database and the data seeding worked fine. So yeah, the users are correctly assigned to their respective roles... – Jimmy May 16 '19 at 06:43
  • 2
    Hi @Jimmy have you try add this to services?`services.AddScoped, UserClaimsPrincipalFactory>();` – Alexandre Rodrigues May 16 '19 at 07:41
  • Thank you so much man! I didn't see you'd edited your solution. I just added `services.AddScoped, UserClaimsPrincipalFactory>();` and it works like a charm. Thanks man:) – Jimmy May 16 '19 at 08:42