日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

小白开学Asp.Net Core 《八》

發(fā)布時(shí)間:2023/12/4 asp.net 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 小白开学Asp.Net Core 《八》 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

小白開學(xué)Asp.Net Core 《八》

            — — .Net Core 數(shù)據(jù)保護(hù)組件

1、背景

  我在搞(https://github.com/AjuPrince/Aju.Carefree)這個(gè)開源項(xiàng)目的時(shí)候,想做一些防止惡意攻擊的小功能(如果 我通過頁面 /Dome/GetData?id=123,那是不是不安全呢?是的,我完全可以嘗試著給id賦值后去獲取數(shù)據(jù))怎么辦呢?在.Net Core 中又給如何處理呢?

2、.Net Core 的數(shù)據(jù)保護(hù)組件

  1、嘗試著在.Net Core 的內(nèi)部擴(kuò)展方法中發(fā)現(xiàn)

  我們都知道在 .Net Core 中注冊(cè)服務(wù),都是在 Startup->ConfigureServices 這個(gè)方式中 通過?services.AddXXXX 來添加的,我也嘗試著看看 .Net Core 有無內(nèi)置的數(shù)據(jù)保護(hù)組件,就利用 VS的智能提示功能 輸入 server.Add 一個(gè)個(gè)去看,結(jié)果就被我我發(fā)現(xiàn)了(開心地像個(gè)孩子 哈哈)

? ? ? ? ? ? ??

F12 進(jìn)去后

通過它的注釋(Extension methods for setting up data protection services in an Microsoft.Extensions.DependencyInjection.IServiceCollection.)(譯成中文:在Microsoft.Extensions.DependencyInjection.IServiceCollection設(shè)置數(shù)據(jù)保護(hù)服務(wù)的擴(kuò)展方法)。

好,既然找到了,那我們就來學(xué)習(xí)下它(我們?cè)撊绾问褂盟?#xff09;。

  2、學(xué)習(xí)、使用

  

 既然知道了(.Net Core 內(nèi)置了數(shù)據(jù)保護(hù)組件),那我也就在類試圖中去找它了,最終還是被我給找見了。(好不廢話了)

? 我們通過上圖可以知道 .Net Core 內(nèi)置了一個(gè) IDataProtectionProvider ?接口 和 IDataProtector 接口,其中 IDataProtectionProvider 接口是創(chuàng)建保護(hù)組件的接口,IDataProtector 是數(shù)據(jù)保護(hù)的接口,我們可以實(shí)現(xiàn)這兩個(gè)接口,創(chuàng)建數(shù)據(jù)保護(hù)組件。

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

?

?同樣的方法,可以去看看,另一個(gè)接口。

下面就讓我們來使用它。

1)、新建一個(gè)類

public class DataDemoViewModel { public int Id { get; set; } public string Name { get; set; } public DataDemoViewModel(int id, string name) { Id = id; Name = name; } }

2)、創(chuàng)建模擬數(shù)據(jù)

public class DemoController : Controller { private List<DataDemoViewModel> _listDataProtect = new List<DataDemoViewModel>(); public DemoController(){ //創(chuàng)建模擬數(shù)據(jù) for (int i = 0; i < 6; i++) { _listDataProtect.Add(new DataDemoViewModel(i, "Aju_Carefree" + i)); } } }

3)、在Startup類的ConfigureService方法中添加服務(wù)

services.AddDataProtection();

4)、在DemoController中? DI注入

public class DemoController : Controller { private List<DataDemoViewModel> _listDataProtect = new List<DataDemoViewModel>(); private readonly IDataProtector _dataProtector; public DemoController(IDataProtectionProvider dataProtectionProvider) { //創(chuàng)建模擬數(shù)據(jù) for (int i = 0; i < 6; i++) { _listDataProtect.Add(new DataDemoViewModel(i, "Aju_Carefree" + i)); } _dataProtector = dataProtectionProvider.CreateProtector("aju_carefree_string"); } }

5)、使用

#region 數(shù)據(jù)保護(hù)組件Demo public IActionResult ProtectIndex() { var outputModel = _listDataProtect.Select(item => new { //使用 IDataProtector 接口的 Protect 方法對(duì)id字段進(jìn)行加密 Id = _dataProtector.Protect(item.Id.ToString()), item.Name }); return Ok(outputModel); } public IActionResult GetProtect(string id) { //使用 IDataProtector 接口的 Unprotect 方法對(duì)id字段進(jìn)行解密 var orignalId = int.Parse(_dataProtector.Unprotect(id)); var outputModel = _listDataProtect.Where(s => s.Id == orignalId); return Ok(outputModel); } #endregion

?6)結(jié)果展示

  (1)請(qǐng)求?/Demo/ProtectIndex

?刷新頁面,id 值是變的。?

(2)、請(qǐng)求 /Home/GetProtect?id=(id取了上圖中的第一個(gè)(框框圈的))

說明:

  (1):使用Provider創(chuàng)建Protector 的時(shí)候,我們傳入了一個(gè)參數(shù)“aju_carefree_string”,這個(gè)參數(shù)標(biāo)明了這個(gè)保護(hù)器的用途,也可以當(dāng)作這個(gè)保護(hù)器的名字。(不同用途的保護(hù)器,不能解密對(duì)方方加密的數(shù)據(jù))

  (2):還有一個(gè)類型的數(shù)據(jù)保護(hù)組件(ITimeLimitedDataProtector(帶過期時(shí)間的數(shù)據(jù)保護(hù)器))就不在這里做說明了,用法差不多。

  (3):本篇文章,只是對(duì)數(shù)據(jù)保護(hù)組件的拋磚引玉(不只是說 它的用法就只能這么用,完全可以有別的用法。)

  (4):本文的代碼全部上傳至Github(https://github.com/AjuPrince/Aju.Carefree)(DemoController )

?參考文章:

  https://docs.microsoft.com/zh-cn/aspnet/core/security/data-protection/introduction?view=aspnetcore-3.0

?

?下一篇 需求了解些什么呢?留言(博客園)哦!(我會(huì)從留言最多中選擇一個(gè)內(nèi)容來分享 我的看法及使用(當(dāng)然前提是我會(huì)哦 哈哈))

?  本人有意組建蘭州線下.Net 開發(fā)社區(qū),有意者加群(QQ:649708779)如果條件允許的話,將會(huì)在8月中旬,組織個(gè)活動(dòng)(只是有這個(gè)想法)


總結(jié)

以上是生活随笔為你收集整理的小白开学Asp.Net Core 《八》的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。