Inside my asp.net mvc 4.0 application, I’m creating a custom AuthorizeAttribute
called “AllowAttribute”. In a nutshell, I wish to apply this attribute on certain methods (not on entire controllers) and allow that attribute to receive a bitwise fashion parameter.
I use the attribute like so (note: MemberType
is an Enum)
[Allow(MyProperty = MemberType.User | MemberType.Administrator)]
The attribute itself is defined like so:
public class AllowAttribute : AuthorizeAttribute
{
public MemberType MyProperty { get; set; }
public override void OnAuthorization(AuthorizationContext filterContext)
{
if (MemberContext.Current.Member == null || MemberContext.Current.Member.MemberType != this.MyProperty)
throw new HttpException(403, "Forbidden");
base.OnAuthorization(filterContext);
}
}
I’m basically trying to compare the current logged in user’s MemberType with the one (or ones) passed inside the MyProperty. If the current logged in user does not match the values passed in parameter, I throw a forbidden exception.
I need some guidance on the bitwise comparison because the simple (not equal to) does not work if more than one Enum value is passed to the attribute.
Thanks