Multiple Authorization attributes on method

后端 未结 4 444
礼貌的吻别
礼貌的吻别 2020-12-08 06:12

I\'m having trouble specifying two separate Authorization attributes on a class method: the user is to be allowed access if either of the two attributes are true.

Th

4条回答
  •  攒了一身酷
    2020-12-08 06:48

    Multiple AuthorizeAttribute instances are processed by MVC as if they were joined with AND. If you want an OR behaviour you will need to implement your own logic for checks. Preferably implement AuthAttribute to take multiple roles and perform an own check with OR logic.

    Another solution is to use standard AuthorizeAttribute and implement custom IPrincipal that will implement bool IsInRole(string role) method to provide 'OR' behaviour.

    An example is here: https://stackoverflow.com/a/10754108/449906

提交回复
热议问题