I write a demo to achieve the "filter"
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace CustomActionFilter.Filter
{
public class MySampleActionFilter : IActionFilter
{
public void OnActionExecuting(ActionExecutingContext context)
{
var secret = context.HttpContext.Request.Form["secret"].ToString();
string privateKey = "MIICxxxxxxxxxxxxxxxxZg==";
var privateKeyBits = Convert.FromBase64String(privateKey);
RSA rsa = RSA.Create();
var rsaParameters = new RSAParameters();
BinaryReader binr = new BinaryReader(new MemoryStream(privateKeyBits));
byte bt = 0;
ushort twobytes = 0;
twobytes = binr.ReadUInt16();
if (twobytes == 0x8130)
binr.ReadByte();
else if (twobytes == 0x8230)
binr.ReadInt16();
else
throw new Exception("Unexpected value read binr.ReadUInt16()");
twobytes = binr.ReadUInt16();
if (twobytes != 0x0102)
throw new Exception("Unexpected version");
bt = binr.ReadByte();
if (bt != 0x00)
throw new Exception("Unexpected value read binr.ReadByte()");
rsaParameters.Modulus = binr.ReadBytes(GetIntegerSize(binr));
rsaParameters.Exponent = binr.ReadBytes(GetIntegerSize(binr));
rsaParameters.D = binr.ReadBytes(GetIntegerSize(binr));
rsaParameters.P = binr.ReadBytes(GetIntegerSize(binr));
rsaParameters.Q = binr.ReadBytes(GetIntegerSize(binr));
rsaParameters.DP = binr.ReadBytes(GetIntegerSize(binr));
rsaParameters.DQ = binr.ReadBytes(GetIntegerSize(binr));
rsaParameters.InverseQ = binr.ReadBytes(GetIntegerSize(binr));
rsa.ImportParameters(rsaParameters);
var temp = Encoding.UTF8.GetString(rsa.Decrypt(Convert.FromBase64String(secret), RSAEncryptionPadding.Pkcs1));
//JObject json = JObject.Parse(temp);
context.ActionArguments["secret"] = temp;
}
public void OnActionExecuted(ActionExecutedContext context)
{
var res = context.Result;
//encrypt code
}
private int GetIntegerSize(BinaryReader binr)
{
byte bt = 0;
int count = 0;
bt = binr.ReadByte();
if (bt != 0x02)
return 0;
bt = binr.ReadByte();
if (bt == 0x81)
count = binr.ReadByte();
else
if (bt == 0x82)
{
var highbyte = binr.ReadByte();
var lowbyte = binr.ReadByte();
byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
count = BitConverter.ToInt32(modint, 0);
}
else
{
count = bt;
}
while (binr.ReadByte() == 0x00)
{
count -= 1;
}
binr.BaseStream.Seek(-1, SeekOrigin.Current);
return count;
}
}
}
and set DI in startup.cs
public void ConfigureServices(IServiceCollection services)
{
//services.AddScoped<MySampleActionFilter>();
services.AddControllersWithViews();
services.AddControllers(config =>
{
config.Filters.Add<MySampleActionFilter>();
});
}
my view :
@{
ViewData["Title"] = "Home Page";
}
<button id="test">send</button>
<script src="https://cdn.bootcdn.net/ajax/libs/jsencrypt/3.2.1/jsencrypt.min.js"></script>
<script>
var user = {
"age": 18,
"name": "tiny"
};
var public_key = "MIGfMA0GCxxxxxxxxxDAQAB";
var private_key = "MIICWwIxxxxxxxxxlRbEbsDVZg==";
var encrypt = new JSEncrypt();
encrypt.setPublicKey(public_key);
var encrypted = encrypt.encrypt(JSON.stringify(user));
console.log(encrypted);
var decrypt = new JSEncrypt();
decrypt.setPrivateKey(private_key);
var uncrypted = decrypt.decrypt(encrypted);
console.log(uncrypted);
$('#test').click(function () {
var user = {
"age": 18,
"name": "tiny"
};
$.ajax({
url: "https://localhost:44369/hello",
type: "post",
data: {
secret: encrypted
},
success: function (data) {
alert(data);
}
})
});
</script>
My controller action:
[HttpPost]
[ServiceFilter(typeof(MySampleActionFilter))]
public JsonResult Index(string secret)
{
User json12 = JsonConvert.DeserializeObject<User>(secret);
User res = new User
{
age = 0,
name = "zero"
};
return Json(res);
}
Test result:
