asp.net mvc 用多个枚举装饰 [Authorize()] [英] asp.net mvc decorate [Authorize()] with multiple enums

查看:29
本文介绍了asp.net mvc 用多个枚举装饰 [Authorize()]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器,我希望有两个角色能够访问它.1 位管理员或 2 位版主

I have a controller and I want two roles to be able to access it. 1-admin OR 2-moderator

我知道你可以做 [Authorize(Roles="admin, moderators")] 但我在枚举中有我的角色.使用枚举我只能授权一个角色.我不知道如何授权两个.

I know you can do [Authorize(Roles="admin, moderators")] but I have my roles in an enum. With the enum I can only authorize ONE role. I can't figure out how to authorize two.

我尝试过类似 [Authorize(Roles=MyEnum.Admin, MyEnum.Moderator)] 的方法,但无法编译.

I have tried something like [Authorize(Roles=MyEnum.Admin, MyEnum.Moderator)] but that wont compile.

曾经有人建议这样做:

 [Authorize(Roles=MyEnum.Admin)]
 [Authorize(MyEnum.Moderator)]
 public ActionResult myAction()
 {
 }

但它不能用作 OR.我认为在这种情况下,用户必须同时成为两个角色的一部分.我是否忽略了一些语法?或者这是我必须滚动自己的自定义授权的情况?

but it doesn't work as an OR. I think in this case the user has to be part of BOTH roles. Am I overlooking some syntax? Or is this a case where I have to roll my own custom authorization?

推荐答案

尝试使用位 OR 运算符,如下所示:

Try using the bit OR operator like this:

[Authorize(Roles= MyEnum.Admin | MyEnum.Moderator)]
public ActionResult myAction()
{
}

如果这不起作用,你可以自己动手.我目前只是在我的项目中做到了这一点.这是我所做的:

If that doesn't work, you could just roll your own. I currently just did this on my project. Here's what I did:

public class AuthWhereRole : AuthorizeAttribute
{
    /// <summary>
    /// Add the allowed roles to this property.
    /// </summary>
    public UserRole Is;

    /// <summary>
    /// Checks to see if the user is authenticated and has the
    /// correct role to access a particular view.
    /// </summary>
    /// <param name="httpContext"></param>
    /// <returns></returns>
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        if (httpContext == null)
            throw new ArgumentNullException("httpContext");

        // Make sure the user is authenticated.
        if (!httpContext.User.Identity.IsAuthenticated)
            return false;

        UserRole role = someUser.Role; // Load the user's role here

        // Perform a bitwise operation to see if the user's role
        // is in the passed in role values.
        if (Is != 0 && ((Is & role) != role))
            return false;

        return true;
    }
}

// Example Use
[AuthWhereRole(Is=MyEnum.Admin|MyEnum.Newbie)]
public ActionResult Test() {}

此外,请确保为您的枚举添加一个标志属性,并确保它们的值都从 1 起.像这样:

Also, make sure to add a flags attribute to your enum and make sure they are all valued from 1 and up. Like this:

[Flags]
public enum Roles
{
    Admin = 1,
    Moderator = 1 << 1,
    Newbie = 1 << 2
    etc...
}

左移给出值 1、2、4、8、16 等.

The left bit shifting gives the values 1, 2, 4, 8, 16 and so on.

好吧,我希望这会有所帮助.

Well, I hope this helps a little.

这篇关于asp.net mvc 用多个枚举装饰 [Authorize()]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆