我希望通过使用 Windsor IInterceptor 在 MVC 应用程序中处理授权 - 因为这似乎是我可以获得对参数的命名访问的唯一方法,传递的操作与确定用户是否具有访问权限相关。
从我的 Intercept 方法中,我需要访问被调用的操作。我想出了如何获取控制器和操作名称(通过 RequestContext),但不是实际的方法 - 有什么好主意吗?
作为参考,这大致是代码的样子:
public class AuthorizationInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
if (invocation.Arguments != null && invocation.Arguments.Length > 0 && invocation.Arguments[0] != null)
{
if (invocation.Arguments[0].GetType() == typeof(RequestContext))
{
var context = (RequestContext)invocation.Arguments[0];
var values = context.RouteData.Values;
if (!auth.Authorize(values, HttpContext.Current.User))
{
//RedirectToLogin }
}
}
invocation.Proceed();
}
}