如何授权一组控制器而不在每个控制器上放置注释? [英] How to authorize a set of controllers without placing the annotation on each one?

查看:23
本文介绍了如何授权一组控制器而不在每个控制器上放置注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组控制器,每个控制器都用于每种授权类型.例如,A 类授权将有一组控制器,每个控制器都需要 A 类授权.有没有办法在某个地方放置一个 [Authorize(Role="Class A")] 属性,该属性将应用于每个控制器,而不必用相同的属性装饰每个控制器?

I have sets of controllers which are each used for each authorization type. For example, a class A authorization will have a set of controllers each which require class A authorization. Is there a way to place one [Authorize(Role="Class A")] attribute somewhere which will apply to each of those controllers without having to decorate each controller with the same attribute?

推荐答案

您可以初始化从您的基本控制器派生的那些控制器.即把你的属性放在一个控制器基类上,并确保每个控制器都从基类派生出来.

You can initialize those controllers derived from your base controller. namely put your attribute on a controller base class and to ensure that each controller within derived from base class.

[Authorize(Role="Class A")]
public class CustomBaseController : Controller{}

public class AController: CustomBaseController{}

public class BController: CustomBaseController{}

这篇关于如何授权一组控制器而不在每个控制器上放置注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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