码迷,mamicode.com
首页 > Windows程序 > 详细

webAPI过滤器返回数据加密

时间:2018-01-30 17:01:25      阅读:318      评论:0      收藏:0      [点我收藏+]

标签:cat   data   amp   body   action   lis   err   etc   rri   

项目需求:

  接口返回的数据,存在一些敏感信息,不希望其他用户看到,将Data进行加密传输

代码如下:

    public class EncryptDataFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {

            base.OnActionExecuted(actionExecutedContext);

            var actionList=actionExecutedContext.ActionContext.ActionDescriptor.GetCustomAttributes<EncryptDataAttribute>();
            var controllList= actionExecutedContext.ActionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<EncryptDataAttribute>();

            if (actionList.Any()||controllList.Any())
            {
                if (((System.Net.Http.ObjectContent)actionExecutedContext.Response.Content).Value is AjaxResCode)
                {
                    AjaxResCode result = actionExecutedContext.Response.Content.ReadAsAsync<AjaxResCode>().Result;
                    if (result.Data!=null&&!string.IsNullOrWhiteSpace(result.Data.ToString()))
                    {
                        string data= Newtonsoft.Json.JsonConvert.SerializeObject(result.Data);
                        //数据加密过程
                        result.Data = AesEncrypt.Encrypt(data, "123456");
                        string res = Newtonsoft.Json.JsonConvert.SerializeObject(result);

                        HttpResponseMessage response = new HttpResponseMessage { Content = new StringContent(res, Encoding.GetEncoding("UTF-8"), "application/json") };
                        actionExecutedContext.Response = response;
                    }
                }
            }
        }
    }    

 

webAPI过滤器返回数据加密

标签:cat   data   amp   body   action   lis   err   etc   rri   

原文地址:https://www.cnblogs.com/zhuyapeng/p/8384168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!