Multiple roles in 'User.IsInRole'

Joan

I have 3 roles on my page, so I want to get access to link with two roles.

I try something like this

@if(User.IsInRole("Admin,User")) 
{
 //my code
}

Or this

@if (User.IsInRole("Admin") && User.IsInRole("User"))

{
 //my code
}

No one work's, the only one who I managed to works is this:

 @if (User.IsInRole("Admin")

But this last one it's only for one Role, How can I do that I want?

Christos

No one work's, the only one who I managed to works is this:

This is reasonable, if you consider what the method IsInRole does.

Gets a value indicating whether the currently logged-on user is in the specified role. The API is only intended to be called within the context of an ASP.NET request thread, and in that sanctioned use case it is thread-safe.

That being said a user may has the role of Admin (so UserIsInRole("Admin") returns true) but may not has the role of User (so UserIsInRole("User") returns false). So User.IsInRole("Admin") && User.IsInRole("User") would evaluate to false.

That you might need is this:

// If the user's role is either admin or user then do something
@if (User.IsInRole("Admin") || User.IsInRole("User"))
{
    //my code
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

multiple User.IsInRole on same page

Multiple User Roles in Authorize

mvcSiteMap Bootstrap showing links based on user roles without using User.IsInRole

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

User with multiple roles and multiple teams database design

Multiple Roles for a User belonging to multiple Organizations

how to Assign Multiple Roles for a user in wordpress?

API - Multiple requests vs separating user roles

Is it possible to create a MongoDb user with multiple roles in Java?

MongoDB Roles - user access multiple databases

How to assign Multiple roles to a single user in wordpress

Rolify Assign Multiple User Roles at Once

Middleware on route level based on multiple user roles

Usage of User.IsInRole() in a View

User.IsInRole return false

Why @User.IsInRole not working?

User.IsInRole is not working on server

Return multiple roles for user in 1 column not multiple rows

How to create multiple roles for single user in ruby on rails?

Can a single Gherkin scenario have multiple user roles?

Integrating Active Directory in Windows Server with Laravel 5 for multiple user roles

Is it possible to assign multiple roles to a user or group in Azure AD?

How to Assign a Gateway Fee in WooCommerce to Multiple User Roles?

Need help checking and adding user to multiple roles async

Model design for a user who can have roles at multiple organizations

how to add multiple roles to an admin user in Apache tomcat 8?

how to add multiple roles to an admin user in Apache tomcat 8?

Showing multiple roles assigned to a user in Laravel using Spatie Permissions

How to implement multiple user types with different roles and permissions in Django?