using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace DemoReact
{
public class Middlewarecustom
{
private readonly RequestDelegate _next;
public Middlewarecustom(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context) {
using (var buffer = new MemoryStream()) {
var stream = context.Response.Body;
context.Response.Body = buffer;
await _next.Invoke(context);
buffer.Seek(0, SeekOrigin.Begin);
var reader = new StreamReader(buffer);
using (var bufferReader = new StreamReader(buffer)) {
string body = await bufferReader.ReadToEndAsync();
WeatherForecast wf …Run Code Online (Sandbox Code Playgroud) 目前我正在向 asp.net 核心控制器发送加密请求。以下是请求体结构:
{
"body": "pEbXVvl1ue95eGQywK/q80cGHjYk+2VNPYEgnRgU+vI="
}
Run Code Online (Sandbox Code Playgroud)
在此之前,我已经实现了过滤器,IActionFilter所以在OnActionExecuting(ActionExecutingContext context)那里我解密了上面提到的请求正文(正文密钥)
解密后,我得到解密后的请求体,如下所示:
{
"Urno":"URN123456"
}
Run Code Online (Sandbox Code Playgroud)
解密后,我想将解密的请求(如上所述"Urno":"URN123456")主体传递给控制器
我曾尝试将字符串转换为字节而不是将其传递给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, …Run Code Online (Sandbox Code Playgroud) 我想在 JavaScript 中屏蔽电子邮件地址 abcdef@gmail.com=> abXXX@gmail.com
我使用了 /(?<=.{2}).(?=[^@]*?@)/ 正则表达式,但在 Internet Explorer 和 Mozilla 中不起作用,所以我需要在所有浏览器中工作的正则表达式(JavaScript)
maskedEmail = stringObj.replace(/(?<=.{2}).(?=[^@]*?@)/g, "X");