我将控制器操作设置为:
[HttpPost]
[DecryptBodyFilterAttribute]
public InitializeSessionResponse InitializeSession([FromBody] InitializeSessionRequest request)
{
...
}
http主体是json并自动被反序列化到我的请求对象中就好了。现在我想让http正文内容成为加密字符串,我将在ActionFilterAtttribute
中解密。加密/解密不是问题。我的问题是在进入控制器中的操作之前将body的值重置为新值。
我的ActionFilterAttribute如下所示:
public sealed class DecryptBodyFilterAttribute : ActionFilterAttribute
{
public DecryptBodyFilterAttribute()
{}
public override Task OnActionExecutingAsync(HttpActionContext actionContext, System.Threading.CancellationToken cancellationToken)
{
string encryptedString = actionContext.Request.Content.ReadAsStringAsync().Result;
//... decrypt do whatever and end up with the new content below...
string newContent = "Something here would be the decrypted json of the initializeSessionRequest";
//I need to do something like the below
//actionContext.Request.Content = newContent;
return base.OnActionExecutingAsync(actionContext, cancellationToken);
}
}
我的问题类似于Modify HttpContent (actionExecutedContext.Response.Content) in OnActionExecuted method of WebApi's ActionFilterAttribute,但是在转换response.content(在我的情况下请求中)时,我得到一个null对象,因此对我来说不起作用。