User.IsInRole return false

Hakim Belekacemi

I 'm using Identity 2 for authentication in mvc 5 web site. In my view i want check the role of the user :

@if(User.IsInRole("Customers"))
{
  @*do something*@
}

but this always return false, I have already set <roleManager enabled="true" /> in the web config. any help please.

Luke

I just got it to work with my setup that is also using Identity Framework.

I added a user to a role by using the following code:

this.RoleManager.CreateAsync(new Role() {Name  = "Customers"});

this.UserManager.AddToRoleAsync(this.User.Identity.GetUserId<int>(), "Amazing");

Then any time after that, when I ran User.IsInRole("Customers"); it returned false, that was until I relogged them back in.

You need to re-log in the user after having added the user to the role. The role information is stored in the cookies.

I ran the following to log the user again:

var user = await this.UserManager.FindByNameAsync("bob");
var identity = await this.UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);

this.AuthManager.SignIn(new AuthenticationProperties() { IsPersistent = true }, identity);

From this point, User.IsInRole("Customers") worked for me and returned true.

This won't work though unless you can verify within your application that it is aware of the role that you want to add them to. You can verify the existence of the role "Customers" by using your RoleManager in the following way:

var roleExists = (this.RoleManager.FindByNameAsync("Customers").Result != null);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

return false the same as return?

Usage of User.IsInRole() in a View

Multiple roles in 'User.IsInRole'

IsInRole return false even if there is role in claims

Using User.IsInRole returns random result when UserRole changes

User.IsInRole() always returns false only in controller

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

User.IsInRole always returns false with Token Authentication

Mock My.User.IsInRole() error in Asp.Net MVC

User.IsInRole() returns false and Authorize Roles gives me an Access Denied

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

Can't check user role. User.IsInRole returning false

User IsInRole only works after signing in and out from the website

.NET: User.IsInRole not working from within Visual Studio

User.IsInRole is not working on server

Why @User.IsInRole not working?

Always return false when i authenticate an user

MVC 4: User.IsInRole() returns false after logout

multiple User.IsInRole on same page

How to use User.IsInRole() in an HTML string?

@if (User.IsInRole("System Administrator","Administrator"))

User.IsInRole is true in view, but if body isn't shown

Search mySQL table for user and return true or false to $result?

Django form.isValid() always return false in custom user model

Is there a way to return true if the user allows location sharing and false if denied?

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

_userManager.IsInRoleAsync(user, roleName) just return false

c# User.IsInRole fails on unit test

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