ActionContext.ActionArguments在IAuthenticationFilter中为空

本文关键字:IAuthenticationFilter ActionArguments ActionContext | 更新日期: 2023-09-27 18:10:54

背景:

我想使用使用Ninject注入的IAuthenticationFilter实现来验证对web API的POST请求。为了验证请求,我需要访问请求主体。

问题:

ActionContext.ActionArguments,我通常用来访问请求负载,当我尝试在过滤器内访问它时,它是空的。

问题:

  1. 如何访问IAuthenticationFilter实现中的POST请求负载
  2. 为什么ActionContext.ActionArgumentsIAuthenticationFilter实现中是空的,但如果我的过滤器实现ActionFilterAttribute,它却有值

代码:

过滤器实现:

public class AuthenticateFilter : IAuthenticationFilter
{
    private const string AuthenticationHeader = "X-Auth-Token";
    private const string UserHeader = "X-Auth-User";
    private readonly ILog log;
    public AuthenticateFilter(ILog log)
    {
        this.log = log;
    }
    public Task AuthenticateAsync(HttpAuthenticationContext context, 
                                  CancellationToken cancellationToken)
    {
        // context.ActionContext.ActionArguments is empty
        if (!IsAuthenticated(context))
        {
            context.ErrorResult = 
                new StatusCodeResult(HttpStatusCode.Unauthorized, 
                                     context.Request);
        }
        return Task.FromResult(0);
    }
    public Task ChallengeAsync(HttpAuthenticationChallengeContext context,
                               CancellationToken cancellationToken)
    {
        context.Result = 
            new StatusCodeResult(HttpStatusCode.Unauthorized, 
                                 context.Request);
        return Task.FromResult(0);
    }
    private bool IsAuthenticated(HttpAuthenticationContext context)
    {
        // Authentication code here
        // context.ActionContext.ActionArguments is empty
    }
}

当控制器方法具有属性时,使用Ninject注入过滤器。

kernel.BindHttpFilter<AuthenticateFilter>(FilterScope.Action)
      .WhenActionMethodHas<AuthenticateAttribute>();

CCD_ 7是空的CCD_。

public class AuthenticateAttribute : ActionFilterAttribute
{
}

谢谢!

ActionContext.ActionArguments在IAuthenticationFilter中为空

这是预期行为。AuthenticationAuthorization过滤器在ModelBinding/Formatter反序列化阶段之前运行,其中Action过滤器在此阶段之后运行。

我自己也遇到了同样的情况,如果这对任何人都有帮助,你需要使用反射和系统。网状物Helpers的Json。解码:

 public Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
    {
        HttpRequestMessage request = context.Request;
        var content = request.Content.ReadAsAsync(typeof(Object)).Result.ToString();
        var methodInfo = ((ReflectedHttpActionDescriptor)request.Properties["MS_HttpActionDescriptor"]).MethodInfo; // get the method descriptor
        if (methodInfo.GetParameters().Any())  //this will get the parameter types
        {
             var parameterType = methodInfo.GetParameters().First().ParameterType; //you iterate can through the parameters if you need
             var casted = Json.Decode(content, parameterType);  //convert the json content into the previous type (your parameter)
             //do something with your populated object :)
        }
        return Task.FromResult(context.Request);
    }
相关文章:
  • 没有找到相关文章