问题描述
我用饰有[国旗]枚举控制在我的MVC2应用程序autoization。下面是我的code的例子:
I'm using an Enum decorated with [Flags] to control autoization within my MVC2 app. Below is my code examples:
[Flags]
public enum SecurityRoles
{
None = 0,
Executive = 1,
BackOffice = 2,
AccountManager = 4,
Consultant = 8,
Administrator = 16
}
[TestMethod]
public void MultipleSelectionsTest()
{
var requiredRoles = SecurityRoles.Executive | SecurityRoles.BackOffice;
var user1Roles = SecurityRoles.Executive | SecurityRoles.Administrator | SecurityRoles.BackOffice | SecurityRoles.Consultant;
var user1HasAccess = user1Roles.HasFlag(requiredRoles);
var user2Roles = SecurityRoles.Administrator | SecurityRoles.BackOffice | SecurityRoles.Consultant;
var user2HasAccess = user2Roles.HasFlag(requiredRoles);
Assert.IsTrue(user1HasAccess); //returns true
Assert.IsTrue(user2HasAccess); //returns false
}
正如你所看到的,user2Roles containes的BackOffice作用和requiredRoles还含有的BackOffice作用,但是,user2HasAccess是假的。这是为什么?我在想什么? user1HasAccess是真实的。
As you can see, user2Roles containes BackOffice role and requiredRoles also contains BackOffice role, however, user2HasAccess is false. Why is that? What am I missing? user1HasAccess is true.
推荐答案
纠正我,如果我错了(因为我可以),但是当你进行枚举标志检查,.NET本质上是做的二进制算术的一个整数,再presents的标志的总和。
Correct me if I'm wrong (because I could be), but when you perform Enum flag checks, .NET is essentially doing binary arithmetic on an integer which represents the sum of the flags.
因此,有 SecurityRoles.Administrator | SecurityRoles.BackOffice | SecurityRoles.Consultant
相同或二进制 11010
具有26的值。
So having SecurityRoles.Administrator | SecurityRoles.BackOffice | SecurityRoles.Consultant
is the same as having a value of 26 or 11010
in binary.
当你拨打电话,以Enum.HasFlags,这是正在执行的操作是返回thisInstance和放大器;标志==标志
When you make the call to Enum.HasFlags, the operation that's being performed is return thisInstance & flag == flag
所以,如果你正在检查pviously提到的标志设置对你的要求 SecurityRoles.Executive的角色$ P $ | SecurityRoles.BackOffice
3或 11二进制
,数学是这样的一个值:
So if you're checking the previously mentioned flag set against your required roles of SecurityRoles.Executive | SecurityRoles.BackOffice
a value of 3 or 11
in binary, the math goes something like this:
11010 - 26 Administrator | BackOffice | Consultant
00011 - 3 Executive | BackOffice
-----
00010 - 2 BackOffice which really doesn't mean anything useful
然后,它会按照 26安培; 3 == 3
是假的。
和为求是彻底的,因为 SecurityRoles.Executive | SecurityRoles.Administrator | SecurityRoles.BackOffice | SecurityRoles.Consultant
的值27或 11011
二进制,数学是这样的:
And for the sake of being thorough, given SecurityRoles.Executive | SecurityRoles.Administrator | SecurityRoles.BackOffice | SecurityRoles.Consultant
a value of 27 or 11011
in binary, the math goes like this:
11011 - 26 Executive | Administrator | BackOffice | Consultant
00011 - 3 Executive | BackOffice
-----
00011 - 3 Executive | BackOffice
然后,它会按照 26安培; 3 == 3
是真实的。
这是扩展方法是这样的,可能是值得的(未经测试)
An extension method something like this, might be worthwhile (untested)
public static bool HasFlags(this Enum source, Enum[] flags)
{
return flags.Any(f => source.HasFlag(f));
}
这篇关于HasFlag不承认角色分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!