日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
如何在ASP.NetCore中實(shí)現(xiàn)數(shù)據(jù)保護(hù)API

本文轉(zhuǎn)載自微信公眾號(hào)「碼農(nóng)讀書  」,作者碼農(nóng)讀書 。轉(zhuǎn)載本文請(qǐng)聯(lián)系 碼農(nóng)讀書 公眾號(hào)。

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站建設(shè)、做網(wǎng)站、江源網(wǎng)絡(luò)推廣、微信平臺(tái)小程序開發(fā)、江源網(wǎng)絡(luò)營銷、江源企業(yè)策劃、江源品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供江源建站搭建服務(wù),24小時(shí)服務(wù)熱線:18980820575,官方網(wǎng)址:www.cdcxhl.com

在 ASP.Net Core 數(shù)據(jù)保護(hù)棧中提供了一種非常簡單的方法來加密API,從而保護(hù)數(shù)據(jù)的安全,通常落地的做法就是 數(shù)據(jù)加密 和 數(shù)據(jù)解密,這篇文章我們就來一起看看如何使用數(shù)據(jù)保護(hù)API。

理解加密和哈希

在安全領(lǐng)域,加密和hash是兩個(gè)非常重要的概念,常常被開發(fā)者混用,其實(shí)這是不對(duì)的,加密是用一種加密算法將一種數(shù)據(jù)轉(zhuǎn)換成另外一種數(shù)據(jù),同時(shí)也要注意,這是一種雙向操作,已加密的數(shù)據(jù)只能通過一個(gè)合適的密鑰去解密,加過密的數(shù)據(jù)又稱為密文,在如今的系統(tǒng)間通訊,數(shù)據(jù)加密還是非常簡單高效的。

相比之下,hash 是一種將 text 轉(zhuǎn)成 消息摘要 的技術(shù),要值得注意的是,hash值是唯一的,這就意味著不同的text文本不可能生成同一個(gè) hash 值,而且還要注意的是,當(dāng) text 轉(zhuǎn)成了 hash 值之后,你很難再將 hash 值再還原成 text 文本。

總的來說,加密是一種雙向技術(shù),可以使用同一個(gè)密鑰對(duì)數(shù)據(jù)進(jìn)行加密解密,hash是一種單向技術(shù),它可以將 text 轉(zhuǎn)成 消息摘要,而這個(gè)摘要很難再還原成原始text。

安裝 Microsoft.AspNetCore.DataProtection

要想使用 數(shù)據(jù)保護(hù)API, 可以使用 Visual Studio 2019 中的 NuGet package manager 可視化界面,還可以用 NuGet package manager console 在命令行窗口中鍵入如下命令。

 
 
 
 
  1. dotnet add package Microsoft.AspNetCore.DataProtection -Version 2.2.0

配置數(shù)據(jù)保護(hù)API

按照 ASP.NET Core 的默認(rèn)慣例,先將 DataProtection 注入到 ServiceCollection 中,如下代碼所示。

 
 
 
 
  1. public class Startup
  2.     {
  3.         // This method gets called by the runtime. Use this method to add services to the container.
  4.         public void ConfigureServices(IServiceCollection services)
  5.         {
  6.             services.AddControllers();
  7.             services.AddDataProtection();
  8.         }
  9.     }

如果你想將加密和解密用到的 密鑰 單獨(dú)存放到文件系統(tǒng)中的話,可以在注入時(shí)稍微修改一下,如下代碼所示:

 
 
 
 
  1. public class Startup
  2.     {
  3.         // This method gets called by the runtime. Use this method to add services to the container.
  4.         public void ConfigureServices(IServiceCollection services)
  5.         {
  6.             services.AddControllers();
  7.             services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(@"D:\IDG\Temp"));
  8.         }
  9.     }

值得注意的是,密鑰 是由 數(shù)據(jù)保護(hù)API 創(chuàng)建和維護(hù),默認(rèn)情況下這個(gè) key 的有效期是 90天,如果你有特殊需求,也可以自己指定 key 的有效期,如下代碼所示:

 
 
 
 
  1. public class Startup
  2.     {
  3.         public void ConfigureServices(IServiceCollection services)
  4.         {
  5.             services.AddControllers();
  6.             services.ConfigureDataProtection(dp =>
  7.             {
  8.                 dp.PersistKeysToFileSystem(new DirectoryInfo(@"D:\IDG\Temp"));
  9.                 dp.SetDefaultKeyLifetime(TimeSpan.FromDays(7));
  10.             });
  11.         }
  12.     }

你甚至可以使用一個(gè)證書來保護(hù)密鑰,也可以直接使用 Azure Key Valult 來存儲(chǔ)密鑰,如果想使用后者,可以用下面的代碼來配置。

 
 
 
 
  1. public class Startup
  2.    {
  3.        public void ConfigureServices(IServiceCollection services)
  4.        {
  5.            services.AddControllers();
  6.            services.AddDataProtection().PersistKeysToAzureBlobStorage(new Uri("Specify the Uri here"))
  7.                                        .ProtectKeysWithAzureKeyVault("keyIdentifier", "clientId", "clientSecret");
  8.        }
  9.    }

數(shù)據(jù)加密

現(xiàn)在 數(shù)據(jù)保護(hù)API 已經(jīng)安裝并配置成功了,接下來看看如何在 Controller 中使用數(shù)據(jù)保護(hù)API。

 
 
 
 
  1. [ApiController]
  2.     [Route("[controller]")]
  3.     public class WeatherForecastController : ControllerBase
  4.     {
  5.         IDataProtector _protector;
  6.         public WeatherForecastController(IDataProtectionProvider provider)
  7.         {
  8.             _protector = provider.CreateProtector(GetType().FullName);
  9.         }
  10.         [HttpGet]
  11.         public string Get()
  12.         {
  13.             var protectedData = _protector.Protect("Hello World");
  14.             return protectedData;
  15.         }
  16.     }

從圖中可以看到,HelloWorld 已經(jīng)被成功加密返回給到前端了,對(duì)了,為了能夠盡量可復(fù)用,可以單獨(dú)用一個(gè)幫助類來做 數(shù)據(jù)保護(hù)API 中的數(shù)據(jù)加密解密,如下代碼所示:

 
 
 
 
  1. public class DataProtectionHelper
  2.     {
  3.         private readonly IDataProtectionProvider _dataProtectionProvider;
  4.         public DataProtectionHelper(IDataProtectionProvider dataProtectionProvider)
  5.         {
  6.             _dataProtectionProvider = dataProtectionProvider;
  7.         }
  8.         public string Encrypt(string textToEncrypt, string key)
  9.         {
  10.             return _dataProtectionProvider.CreateProtector(key).Protect(textToEncrypt);
  11.         }
  12.         public string Decrypt(string cipherText, string key)
  13.         {
  14.             return _dataProtectionProvider.CreateProtector(key).Unprotect(cipherText);
  15.         }
  16.     }

值得注意的是,上面的 Encrypt 和 Decrypt 方法的第二個(gè)參數(shù)是密鑰key,這樣的話 密鑰 的掌控權(quán)就在你的手上了。

數(shù)據(jù)保護(hù)API 使用起來還是非常簡單靈活的,使用這種方法來生成密文數(shù)據(jù)是一種非常好的方法,常見的場(chǎng)景太多了,比如:cookie,querystring 中的數(shù)據(jù),而且在過期時(shí)間之內(nèi)加密解密操作都是安全的,如果你的密文要維持很長的時(shí)間,這種場(chǎng)景下建議自己實(shí)現(xiàn) 加密解密 邏輯。

譯文鏈接:https://www.infoworld.com/article/3431139/how-to-use-the-data-protection-api-in-aspnet-core.html


網(wǎng)頁標(biāo)題:如何在ASP.NetCore中實(shí)現(xiàn)數(shù)據(jù)保護(hù)API
轉(zhuǎn)載源于:http://www.dlmjj.cn/article/cdjocgc.html