C# ActionContext.ActionArguments在IAAuthenticationFilter中为空
背景: 我想使用Ninject注入的C# ActionContext.ActionArguments在IAAuthenticationFilter中为空,c#,asp.net,authentication,asp.net-web-api2,action-filter,C#,Asp.net,Authentication,Asp.net Web Api2,Action Filter,背景: 我想使用Ninject注入的iaauthenticationfilter实现对我的web API的POST请求进行身份验证。要验证请求,我需要访问请求正文 问题: ActionContext.ActionArguments,我通常用来访问请求负载,当我尝试在筛选器中访问它时,它是空的 问题: 如何访问IAuthenticationFilter实现中的请求后有效负载 为什么ActionContext.ActionArguments在IAAuthenticationFilter实现中为空,但
iaauthenticationfilter
实现对我的web API的POST请求进行身份验证。要验证请求,我需要访问请求正文
问题:
ActionContext.ActionArguments
,我通常用来访问请求负载,当我尝试在筛选器中访问它时,它是空的
问题:
IAuthenticationFilter
实现中的请求后有效负载ActionContext.ActionArguments
在IAAuthenticationFilter
实现中为空,但如果我的筛选器实现了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>();
谢谢大家! 这是预期的行为<代码>身份验证和
授权
过滤器在ModelBinding/Formatter反序列化阶段之前运行,其中as操作
过滤器在此阶段之后运行。这是预期行为<代码>身份验证和授权
过滤器在ModelBinding/Formatter反序列化阶段之前运行,其中as操作
过滤器在此阶段之后运行。我自己也遇到了同样的情况,如果它对任何人都有帮助,您需要使用反射和System.Web.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);
}
我自己也遇到过同样的情况,如果它对任何人都有帮助,你需要使用反射和System.Web.Helpers的Json.Decode:
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);
}
(1) 如果您(暂时)不使用Ninject,它是否有效?(2) 如果使用授权筛选器,它是否有效?(3) 可能值得一看。(1)如果您(暂时)不使用Ninject,它是否有效?(2) 如果使用授权筛选器,它是否有效?(3) 也许值得一看,明白了。是否可以在
认证
和授权
过滤器中访问请求正文,或者不建议使用?是的,可以在认证
和授权
过滤器中读取请求正文,但这并不常见……因此,您可以详细描述您的场景……明白了。是否可以在认证
和授权
过滤器中访问请求正文,或者不建议使用?是的,可以在认证
和授权
过滤器中读取请求正文,但这并不常见…因此您可以详细描述一下您的场景…有点老套,但我认为它会起作用:)你是如何主持的?IIS?自我主持?有点老套,但我认为它会起作用:)你是如何主持的?IIS?自我主持?