ASP.NET MVC Authorize user with many roles

Herno

I need to authorize a Controller in my ASP.NET MVC application to users which have two roles. I am using Authorize attribute like this:

[Authorize(Roles = "Producer, Editor")]

But this allows Producers and Editors to the controller. I want only to allow users having both roles, not just one of them.

How could i achive this?

Molomby

As the question states, when multiple roles are given in a single Authorize() call they are applied such that if the user belongs to any of the roles listed they will be granted access; like a logical OR operator.

Alternatively, to achieve the effect of a logical AND operator you can apply the Authorize attribute multiple times. Eg..

[Authorize(Roles = "Producer")]
[Authorize(Roles = "Editor")]
public ActionResult Details(int id) {
    // Only available to users who are Producers AND Editors
}

For the example above, the action body is accessible only to users who belong to the Producer and the Editor roles.

Rudi points out in the comments this lets you create some reasonably complex access rules without needing to implement a custom AuthorizeAttribute. For example, in the code below users can execute the action if they are both: a) in the Enabled role and b) in either the Editor or Admin roles.

[Authorize(Roles = "Enabled")]
[Authorize(Roles = "Editor,Admin")]
public ActionResult Details(int id) {
    // Only available to users who are Enabled AND either an Admin OR an Editor
}

I'm not sure which version brought this in but it works in at least MVC 4 and 5.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ASP.NET MVC 4 Custom Authorize Attribute with Permission Codes (without roles)

Custom user authorization based with roles in asp.net mvc

How to add a simple user roles - ASP.NET MVC C#

ASP.net MVC - Authorize controller for one user/role but all users for one action

Adding custom roles to windows roles in ASP.NET MVC 5

Dynamically add roles to authorize attribute for controller in ASP.NET 5

How to create a Custom Authorize Attribute by comparing User Id stored in table with Current User Id in Asp.net MVC 5?

How to authorize user who logged in using external login in Asp.Net MVC

ASP.Net MVC List of User and their Roles

Asp.Net MVC 5 - Custom Authorize not working?

`[Authorize(Roles = "admin")]` Infinite loop ASP.NET MVC and Azure Active Directory B2C

How [Authorize] attribute get to know that the user is authenticate in ASP.NET MVC, is it by using authentication token?

Asp.Net Core Identity - Authorize attribute with roles and caching?

ASP.Net MVC SimpleMembershipProvider and using Roles

ASP .Net MVC 4 Authorize and AllowAnonymous

How to check user is in many roles in asp.net identity

Multiple User Roles in Authorize

ASP.NET (MVC) Users, Roles and Users in Roles

Custom Authorize Attribute on asp.net mvc

ASP.NET MVC Blacklist for Roles/Users

Regarding Authorize attribute usage in ASP.Net MVC 4

ASP.Net MVC 5 how to use Authorize Attribute with multiple login (Multiple user table)

Asp.Net MVC authorize a custom user which extends ApplicationUser

ASP.NET MVC Displaying a user's roles as comma separated string

Asp.net Core MVC Authorize Attribute not blocking

Many to many relation MVC ASP.NET

How to handle many to many same table (User) in ASP.Net MVC 5 - Fluent API

Generic Authorize Attribute multiple Roles ASP.NET Core

Asp.net Core MVC Roles and Authorization

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive