zoukankan      html  css  js  c++  java
  • webAPI过滤器返回数据加密

    项目需求:

      接口返回的数据,存在一些敏感信息,不希望其他用户看到,将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;
                        }
                    }
                }
            }
        }    
  • 相关阅读:
    搭建家庭无线Adhoc网络
    IPv4到IPv6的过渡技术
    IPV6的安全性
    IPv6技术简要解析
    安全删除和恢复文件的脚本
    什么是 WPS(WiFi Protected Setup)
    华为:IPv6过渡技术中的探索
    IPV6地址设置及使用方法
    部分IIS日志参数名称解释
    《隐秘的角落》
  • 原文地址:https://www.cnblogs.com/zhuyapeng/p/8384168.html
Copyright © 2011-2022 走看看