Зашифровать тело ответа с помощью атрибута в ASP.NETC#

Место общения программистов C#
Anonymous
Зашифровать тело ответа с помощью атрибута в ASP.NET

Сообщение Anonymous »

Я пытаюсь создать атрибут для шифрования ответа API.
Это код атрибута
public class MyCustomFilter : ActionFilterAttribute {
private MemoryStream responseBody;

public override void OnActionExecuting(ActionExecutingContext context) {
responseBody = new MemoryStream();
context.HttpContext.Response.Body = responseBody;
}

public override async void OnResultExecuted(ResultExecutedContext context) {
responseBody.Seek(0, SeekOrigin.Begin);
using (StreamReader sr = new(responseBody)) {
string actionResult = await sr.ReadToEndAsync();
string j = AesOperation.EncryptString("b14ca5898a4e4133bbce2ea2315a1916", actionResult);
Console.WriteLine("LLL");
Console.WriteLine(j);

context.HttpContext.Response.ContentType = "text/plain";
await context.HttpContext.Response.WriteAsync(j);
}

base.OnResultExecuted(context);
}
}

И это API
[HttpGet]
[MyCustomFilter]
public ActionResult Read() => repository.ReadAppSettings();

И вот какую ошибку я получаю
Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HN5FN3JH41NQ", Request id "0HN5FN3JH41NQ:00000001": An unhandled exception was thrown by the application.
System.InvalidOperationException: Response Content-Length mismatch: too few bytes written (0 of 12847).


Подробнее здесь: https://stackoverflow.com/questions/788 ... in-asp-net

Вернуться в «C#»