且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 ASP.NET Core 2.0 中实现 machineKey

更新时间:2023-02-15 08:10:56

您需要使用 DataProtection APIs 现在:

ASP.NET Core 数据保护堆栈提供了一个简单易用的加密 API,开发人员可以使用它来保护数据,包括密钥管理和轮换.

The ASP.NET Core data protection stack provide a simple, easy to use cryptographic API a developer can use to protect data, including key management and rotation.

示例可以在官方 DataProtection repo 中找到.

Samples could be found in official DataProtection repo.

顺便说一下,同样的方法适用于 ASP.NET:在 ASP.NET 中替换

The same approach, by the way, works with ASP.NET: Replacing <machineKey> in ASP.NET

数据保护系统建立在两个核心概念之上 - 数据保护提供程序(由 IDataProtectionProvider 接口表示),用于创建数据保护程序(由 IDataProtector 接口)通过 CreateProtector 方法.数据保护器用于加密和解密数据.

The data protection system is built upon two core concepts - a data protection provider (represented by the IDataProtectionProvider interface), which is used to create a data protector (represented by the IDataProtector interface) by CreateProtector method. The data protector is used to encrypt and decrypt data.

要将 IDataProtectionProvider 注册到 DI 中,请使用 .AddDataProtection 方法:

To register IDataProtectionProvider into DI use .AddDataProtection method:

public void ConfigureServices(IServiceCollection services)
{
    // Adds data protection services
    services.AddDataProtection();
    ...
}