1

我目前(非常简单地)使用以下代码进行拦截:

(见底部的问题)

我的拦截器:

public interface IAuthorizationInterceptor : IInterceptor { }
public class AuthorizationInterceptor : IAuthorizationInterceptor
{


    public IParameter[] AttributeParameters { get; private set; }


    // This doesnt work currently... paramters has no values
    public AuthorizationInterceptor(IParameter[] parameters) {
        AttributeParameters = parameters;
    }

    public void Intercept(IInvocation invocation) {
        // I have also tried to get the attributes like this
        // which also returns nothing.
        var attr = invocation.Request.Method.GetCustomAttributes(true);


        try {
            BeforeInvoke(invocation);
        } catch (AccessViolationException ex) {

        } catch (Exception ex) {
            throw;
        }
        // Continue method and/or processing additional attributes
        invocation.Proceed();
        AfterInvoke(invocation);
    }


    protected void BeforeInvoke(IInvocation invocation) {

        // Enumerate parameters of method call
        foreach (var arg in invocation.Request.Arguments) {
            // Just a test to see if I can get arguments
        }

        //TODO: Replace with call to auth system code.
        bool isAuthorized = true;

        if (isAuthorized == true) {
            // Do stuff               
        }
        else {
            throw new AccessViolationException("Failed");
        }             
    }

    protected  void AfterInvoke(IInvocation invocation) {


    }
}

我的属性:

public class AuthorizeAttribute : InterceptAttribute
{
    public string[] AttributeParameters { get; private set; }

    public AuthorizeAttribute(params string[] parameters) {
        AttributeParameters = parameters;
    }

    public override IInterceptor CreateInterceptor(IProxyRequest request) {
        var param = new List<Parameter>();
        foreach(string p in AttributeParameters) {
            param.Add( new Parameter(p, p, false));
        }
        // Here I have tried passing ConstructorArgument(s) but the result
        // in the inteceptor constructor is the same. 
        return request.Context.Kernel.Get<IAuthorizationInterceptor>(param.ToArray());
    }
}

应用于方法:

[Authorize("test")]
public virtual Result<Vault> Vault(DateTime date, bool LiveMode = true, int? SnapshotId = null)
{
        ...
}

这行得通,我可以像这样通过属性传递其他参数:

[Authorize("test")]

如果您在我的属性中注意到我正在从属性中获取一些参数,我可以在属性类中访问这些参数,但是我无法将这些参数传递给拦截器。我尝试在 Kernel.Get<>() 调用中使用 ConstructorArgument,它不会引发错误,但 AuthorizationInterceptor 构造函数没有从 ninject 获取任何值。正如您在代码示例中看到的那样,我也尝试过 GetCustomAttributes() ,但这也没有返回任何内容。通过查看其他类似的帖子(Ninject Interception 3.0 Interface proxy by method attributes),这似乎是正确的方法,但它不起作用。有任何想法吗?

4

1 回答 1

1

通过在拦截器上创建初始化方法,我能够得到一些工作。我不太喜欢它,因为它将我与 AuthorizationInterceptor 的具体实现联系在一起,但它完成了工作(该死的截止日期哈哈)。我仍然想知道是否有更好的方法来做到这一点,所以我不会标记自己的答案,希望有人能提出更好的方法来做到这一点。

我修改了属性如下:

    public override IInterceptor CreateInterceptor(IProxyRequest request) {
        AuthorizationInterceptor attr = (AuthorizationInterceptor)request.Context.Kernel.Get<IAuthorizationInterceptor>();
        attr.Init(AttributeParameters);
        return attr;
    }

并在拦截器上创建了一个 Init 方法:

    public void Init(params string[] parameters) {
        AttributeParameters = parameters;
    }
于 2013-08-21T14:42:31.360 回答