Esempio n. 1
0
        private async Task HandleExceptionAsync(HttpContext context, int statusCode, string msg, Stream st)
        {
            rsa = new RSAHelper(RSAType.RSA2, Encoding.UTF8, setting.PrivateKey, setting.PublicKey, setting.AppKey, setting.SplitStr);
            var result = new RsaDto()
            {
                Data = rsa.AppEncrypt(new ResponseDto()
                {
                    status = -1, msg = msg
                })
            };
            var ms = new MemoryStream(Encoding.ASCII.GetBytes(JsonSerializer.Serialize(result, options: new JsonSerializerOptions()
            {
                //IgnoreNullValues = true,
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            })));
            await ms.CopyToAsync(st);

            //context.Response.ContentType = "application/json;charset=utf-8";
            //await context.Response.WriteAsync(JsonConvert.SerializeObject(result));
        }
Esempio n. 2
0
        //加解密
        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            var originalBodyStream = context.Response.Body;

            try
            {
                if (context.Request.Path.Value != "/api/Upload/Upload")
                {
                    context.Request.EnableBuffering(); //内存中创建缓冲区存放Request.Body的内容,否则不能使用context.Request.Body.Position;
                                                       //读取请求
                    var requestReader     = new StreamReader(context.Request.Body);
                    var requestContextRsa = await requestReader.ReadToEndAsync();

                    context.Request.Body.Position = 0;//重新开始,否则body为空
                    var conttype = context.Request.ContentType;
                    if (!string.IsNullOrWhiteSpace(requestContextRsa))
                    {
                        var rsaDto = JsonSerializer.Deserialize <RsaDto>(requestContextRsa, new JsonSerializerOptions()
                        {
                            PropertyNameCaseInsensitive = true
                        });
                        var requestContext = rsa.Decrypt(rsaDto.Data);
                        context.Request.Body = new MemoryStream(Encoding.UTF8.GetBytes(requestContext));
                        // await stream.CopyToAsync(context.Request.Body);
                    }
                }
                using var ms          = new MemoryStream();
                context.Response.Body = ms;
                await next(context);

                context.Response.Body.Seek(0, SeekOrigin.Begin);
                var responseStr = await new StreamReader(ms).ReadToEndAsync();
                context.Response.Body.Seek(0, SeekOrigin.Begin);
                var path = context.Request.Path.Value;
                if (!string.IsNullOrWhiteSpace(responseStr) && context.Response.StatusCode == 200)
                {
                    if (!path.EndsWith(".json"))
                    {
                        var result = new RsaDto()
                        {
                            Data = rsa.AppEncrypt(responseStr)
                        };
                        //var array= Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(result,Formatting.Indented, new JsonSerializerSettings{ContractResolver = new CamelCasePropertyNamesContractResolver()}));
                        var array = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(result, options: new JsonSerializerOptions()
                        {
                            //IgnoreNullValues = true,
                            PropertyNamingPolicy = JsonNamingPolicy.CamelCase
                        }));;
                        var newMs = new MemoryStream(array);
                        await newMs.CopyToAsync(originalBodyStream);
                    }
                    else
                    {
                        var array = Encoding.UTF8.GetBytes(responseStr);
                        var newMs = new MemoryStream(array);
                        await newMs.CopyToAsync(originalBodyStream);
                    }
                }
            }
            catch (WeixiaoException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }