小白开学asp.net core 《八》

            — — .net core 数据保护组件

1、背景

  我在搞(https://github.com/ajuprince/aju.carefree)这个开源项目的时候,想做一些防止恶意攻击的小功能(如果 我通过页面 /dome/getdata?id=123,那是不是不安全呢?是的,我完全可以尝试着给id赋值后去获取数据)怎么办呢?在.net core 中又给如何处理呢?

2、.net core 的数据保护组件

  1、尝试着在.net core 的内部扩展方法中发现

  我们都知道在 .net core 中注册服务,都是在 startup->configureservices 这个方式中 通过 services.addxxxx 来添加的,我也尝试着看看 .net core 有无内置的数据保护组件,就利用 vs的智能提示功能 输入 server.add 一个个去看,结果就被我我发现了(开心地像个孩子 哈哈)

              

f12 进去后

通过它的注释(extension methods for setting up data protection services in an microsoft.extensions.dependencyinjection.iservicecollection.)(译成中文:在microsoft.extensions.dependencyinjection.iservicecollection设置数据保护服务的扩展方法)。

好,既然找到了,那我们就来学习下它(我们该如何使用它)。

  2、学习、使用

  

 既然知道了(.net core 内置了数据保护组件),那我也就在类试图中去找它了,最终还是被我给找见了。(好不废话了)

  我们通过上图可以知道 .net core 内置了一个 idataprotectionprovider  接口 和 idataprotector 接口,其中 idataprotectionprovider 接口是创建保护组件的接口,idataprotector 是数据保护的接口,我们可以实现这两个接口,创建数据保护组件。

  (肯定有人问我,我怎么知道的)

 

 同样的方法,可以去看看,另一个接口。

下面就让我们来使用它。

1)、新建一个类

public class datademoviewmodel
    {
        public int id { get; set; }

        public string name { get; set; }

        public datademoviewmodel(int id, string name)
        {
            id = id;
            name = name;
        }
    }

2)、创建模拟数据

 public class democontroller : controller
 {
     private list<datademoviewmodel> _listdataprotect = new list<datademoviewmodel>();

   public democontroller(){
//创建模拟数据 for (int i = 0; i < 6; i++) { _listdataprotect.add(new datademoviewmodel(i, "aju_carefree" + i)); } } }

3)、在startup类的configureservice方法中添加服务

 services.adddataprotection();

4)、在democontroller中  di注入

 public class democontroller : controller
    {
        private list<datademoviewmodel> _listdataprotect = new list<datademoviewmodel>();

        private readonly idataprotector _dataprotector;


        public democontroller(idataprotectionprovider dataprotectionprovider)
        {
         
            //创建模拟数据
            for (int i = 0; i < 6; i++)
            {
                _listdataprotect.add(new datademoviewmodel(i, "aju_carefree" + i));
            }
            _dataprotector = dataprotectionprovider.createprotector("aju_carefree_string");
        }
 }

5)、使用

  #region 数据保护组件demo
        public iactionresult protectindex()
        {
            var outputmodel = _listdataprotect.select(item => new
            {
          //使用 idataprotector 接口的 protect 方法对id字段进行加密 id = _dataprotector.protect(item.id.tostring()), item.name }); return ok(outputmodel); } public iactionresult getprotect(string id) {
       //使用 idataprotector 接口的 unprotect 方法对id字段进行解密 var orignalid = int.parse(_dataprotector.unprotect(id)); var outputmodel = _listdataprotect.where(s => s.id == orignalid); return ok(outputmodel); } #endregion

 6)结果展示

  (1)请求 /demo/protectindex

 

  刷新页面,id 值是变的。 

(2)、请求 /home/getprotect?id=(id取了上图中的第一个(框框圈的))

 

说明:

  (1):使用provider创建protector 的时候,我们传入了一个参数“aju_carefree_string”,这个参数标明了这个保护器的用途,也可以当作这个保护器的名字。(不同用途的保护器,不能解密对方方加密的数据)

  (2):还有一个类型的数据保护组件(itimelimiteddataprotector(带过期时间的数据保护器))就不在这里做说明了,用法差不多。

  (3):本篇文章,只是对数据保护组件的抛砖引玉(不只是说 它的用法就只能这么用,完全可以有别的用法。)

  (4):本文的代码全部上传至github(https://github.com/ajuprince/aju.carefree)(democontroller )

 参考文章:

  

 

  如果觉得写的还不错的话,就点个推荐呗! 哈哈

 下一篇 需求了解些什么呢?留言哦!(我会从留言最多中选择一个内容来分享 我的看法及使用(当然前提是我会哦 哈哈))

   本人有意组建兰州线下.net 开发社区,有意者加群(qq:649708779)如果条件允许的话,将会在8月中旬,组织个活动(只是有这个想法)