简体   繁体   English

如何在asp.net核心的IActionFilter中更改请求体

[英]How to change request body in IActionFilter in asp.net core

Currently I am sending encrypted request to asp.net core controller. 目前我正在向asp.net核心控制器发送加密请求。 The following is the request body structure: 以下是请求体结构:

{
  "body": "pEbXVvl1ue95eGQywK/q80cGHjYk+2VNPYEgnRgU+vI="
}

Before that I had implemented the filter IActionFilter so in OnActionExecuting(ActionExecutingContext context) where I decrypt the above mention request body(body key) 在此之前,我已经在OnActionExecuting(ActionExecutingContext context)中实现了过滤器IActionFilter ,在那里我解密了上面提到的请求体(正文密钥)

After decryption, I got the decrypted request body like below: 解密后,我得到了解密的请求体,如下所示:

{
  "Urno":"URN123456"
}

After decryption, I want to pass decrypted request (mentioned above "Urno":"URN123456" ) body to controller 解密后,我想将解密的请求(上面提到的"Urno":"URN123456" )身体传递给控制器

I had tried to convert the string to byte than pass it to Request.body but no success: 我曾尝试将字符串转换为字节而不是将其传递给Request.body但没有成功:

public void OnActionExecuting(ActionExecutingContext context)
{
    var request = context.HttpContext.Request;
    request.EnableRewind();
    request.Body.Position = 0;
    using (var reader = new StreamReader(request.Body))
    {                   
        var bodyString = reader.ReadToEnd(); //{ "body": "pEbXVvl1ue95eGQywK/q80cGHjYk+2VNPYEgnRgU+vI="}
        if (bodyString != "")
        {
             var data = JObject.Parse(bodyString);
             var key = data.GetValue("body"); 
             var keybytes = Encoding.UTF8.GetBytes("808080808080808011");
             var iv = Encoding.UTF8.GetBytes("8080808080808080111");
             var encrypted = Convert.FromBase64String(key.ToString());
             var decriptedFromJavascript = DecryptStringFromBytes(encrypted, keybytes, iv); //{ "Urno":"URN123456"}
             byte[] bytes = Encoding.ASCII.GetBytes(decriptedFromJavascript);
             request.Body = new MemoryStream(bytes); // here i am trying to change request body
        }
    }
}
   // controller
[HttpPost("[action]")]
public async Task<string> GetInvestorUrno(Urnoinvestor InfoReq){

}


// class
public class Urnoinvestor 
{
    public string Urno{ get; set; }
}

I want to change request body and pass the decrypted request to controller 我想更改请求正文并将解密的请求传递给控制器

For your scenario, IActionFilter would not work. 对于您的场景, IActionFilter不起作用。 Check this image: 检查此图片:

在此输入图像描述

The Model binding is run before ActionFilter which means no matter what you changed to the body, it would not change the model. Model bindingActionFilter之前运行,这意味着无论您更改为正文,它都不会更改模型。 You need to try the filter before Model binding like Resource filter . 您需要在模型绑定之前尝试过滤器,如Resource filter

  1. Change to Resource filter 更改为Resource filter

     public class RequestBodyFilter : IResourceFilter { public void OnResourceExecuted(ResourceExecutedContext context) { //throw new NotImplementedException(); } public void OnResourceExecuting(ResourceExecutingContext context) { var request = context.HttpContext.Request; request.EnableRewind(); request.Body.Position = 0; using (var reader = new StreamReader(request.Body)) { var decriptedFromJavascript = "{ \\"Urno\\":\\"URN123456\\"}"; //{ "Urno":"URN123456"} byte[] bytes = Encoding.ASCII.GetBytes(decriptedFromJavascript); request.Body = new MemoryStream(bytes); // here i am trying to change request body } } } 
  2. Since you are binding from body, you need to specify the action with [FromBody] as the suggestion from @Tony Abrams. 由于你是从身体绑定的,你需要用[FromBody]指定动作作为@Tony Abrams的建议。

     [HttpPost("[action]")] [TypeFilter(typeof(RequestBodyFilter))] public async Task<string> GetInvestorUrno([FromBody]Urnoinvestor InfoReq) { return InfoReq.Urno; } 

You don't have the parameter marked as from body, so it's not loading the data from the request body. 您没有将参数标记为来自正文,因此它不会从请求正文中加载数据。 Instead it's expecting it in the URI. 相反,它期望在URI中。

Try this instead: 试试这个:

[HttpPost("[action]")]
public async Task<string> GetInvestorUrno([FromBody]Urnoinvestor InfoReq){

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM