如何在我的自定义属性中使用按位或运算传递多个参数,与方法或类FeatureAuthorize
一样AttributeUsage
支持。AttributeTarget
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
下面是我想要实现的示例,应该可以访问提供的任何功能,无论是汇款还是收款方法。
[FeatureAuthorize(Feature = EnumFeature.SendMoney | EnumFeature.ReceiveMoney)]
public ActionResult SendOrReceiveMoney(int? id, EnumBankAccountType? type)
{
// My code
}
FeatureAuthorize 属性的主体是这样的。
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class FeatureAuthorizeAttribute : AuthorizeAttribute
{
public EnumFeature Feature { get; set; }
public override void OnAuthorization(AuthorizationContext filterContext)
{
if (!IsFeatureAllowed(Feature)) // Verification in database.
{
// Redirection to not authorize page.
}
}
}
提前致谢。