按功能进行动态授权

时间:2015-01-29 12:20:03

标签: c# .net asp.net-mvc authorization

我有一个基本控制器,它在构造函数中接收功能名称,如下所示:

public abstract class BaseController : Controller
{
   protected string Feature { get; private set;}

   protected BaseController(){ }
   protected BaseController(string feature)
   {
      this.Feature = feature;
   }
}

在我的所有控制器中,我构造如下:

[AuthorizeUser]
public class ProductController : BaseController
{
   public ProductController(IProductService service)
      : base("Product Management")
   {
     ...
   }
}

在我的应用程序中,授权是按功能(而非Action),因此,我创建了一个AuthorizeUser属性并装饰了我的所有控制器。

public class AuthorizeUserAttribute : AuthorizeAttribute
{
   protected override bool AuthorizeCore(HttpContextBase httpContext)
   {
      ....
   }
}

有没有办法在AuthorizeCore 中获取我的基础(在示例中,“产品管理”)中定义的值?

我知道方法必须是线程安全的,所以,我需要一些帮助来解决这个问题。也许使用其他覆盖,如 OnAuthorization OnCacheAuthorization ?我关心的是线程安全性

1 个答案:

答案 0 :(得分:0)

我不确定是否可以做你想做的事。如果可能,我会建议您为AuthorizeUserAttribute创建一个新的construcor并发送该功能。

[AuthorizeUser("MySuperFeature")]

然后使用该功能在AuthorizeCore中进行验证。

相关问题