如何通过AuthorizeAttribute传递合并的枚举?

时间:2019-05-26 17:13:13

标签: c# enums authorize-attribute

我正在尝试从服装化的AuthorizeAttribute中传递合并的枚举角色:

[Flags]
public enum CustomRoles
{
    JournalMgmt = 1,

    Manager = 2,

    PublisherRole = 4,

    StoreMgmt = 8,   

    AdministratorOrUser = Manager | PublisherRole  
}

在我的控制器中,我致电:

    [AuthoriseUser(CustomRoles.AdministratorOrUser)]
    [HttpGet("GetJournalsByIdAsync/")]
    [ProducesResponseType(200, Type = typeof(JournalsGetResponse))]
    [ProducesResponseType(204)]
    [ProducesResponseType(400)]
    public async Task<ActionResult> GetJournalsByIdAsync([Required]int journalId)
    {
        //API work
    }

我无法在自定义的AuthorizationAttribute中检索AdministratorOrUser:

public class AuthoriseUserAttribute : AuthorizeAttribute, IAuthorizationFilter
{
    public AuthoriseUserAttribute(params CustomRoles[] roles) : base()
    {
        //get roles from db  e.g : Manager, JournalMgmt, PublisherRole 
        CRoles = database.ObjectGetTypedListAsync<RoleResponse>(null, "Role");
        foreach (var role in CRoles)
        {
            //check if role from db equals roles in enum AdministratorOrUser
            //e.g : role ="Manager" , Roles = "AdministratorOrUser" where AdministratorOrUser = Manager | PublisherRole
                if (role.Equals(roles))
                {
                    //Authorization code
                }
            }
        }
    }

如何从CustomRoles.AdministratorOrUser获取“ Manager”,“ PublisherRole”?

0 个答案:

没有答案