我如何在资源库中传递给在ASP.NET MVC的授权属性
我是温莎城堡,它在传递正在使用的存储库控制器构造的伟大工程。
I am castle Windsor and it works great for controller constructors in passing in the repository that is being used.
private IStoryRepository Repository;
public StoryController(IStoryRepository Repository)
{
this.Repository = Repository;
}
现在我有一个动作是在管理方面显示主管理菜单。我已经使用了自定义授权属性,这将只是检查登录的用户是管理员(只是在用户表中的isAdmin标志)
Now I have an Action that is in the admin area to display the main admin menu. I have used a custom authorisation attribute which will just check that the logged in user is an admin (just an isAdmin flag in the users table)
[AdminAuthorize]
public ActionResult Menu()
private IStoryRepository Repository;
/// <summary>
/// Initializes a new instance of the <see cref="AdminAuthorizeAttribute"/> class.
/// </summary>
public AdminAuthorizeAttribute(IStoryRepository Repository)
{
this.Repository = Repository;
}
/// <summary>
/// Checks if the user is authorised
/// </summary>
/// <param name="httpContext">The HTTP context.</param>
/// <returns>
/// <c>true</c> if authorized; otherwise, <c>false</c>.
/// </returns>
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
return this.Repository.UserIsAdmin(httpContext.User.Identity.Name);
}
我怎样才能城堡传递到存储库属性构造像它的控制器构造函数?
How can I get Castle to pass the repository into attribute constructor like it does for a controller constructor?
您基本上有两种选择。包裹过滤器代理的一个很好的例子可以发现的这里。
You basically have two options. Wrap the filter in a proxy, a good example of this can be found here.
或者,您的自定义过滤器中,你可以做一个明确的容器调用。例如使用StructureMap(我没有用过城堡广泛)
Or, within your custom filter you can do an explicit container call. For example using StructureMap (I have no used castle extensively)
ObjectFactory.GetInstance(IStoryRepository)
有可能是延长ActionInvoker做注射第三种方式,但我不知道如何做到这一点做的。
There may be a third way which is to extend the ActionInvoker to do the injection but I am not sure how this would be done.