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

2024-01-11

在 ASP.NET(非核心)中,我通常会在 web.config 中添加一个 machineKey,以便我可以在本地计算机而不是服务器上执行一些功能,以便数据库/回调操作将使用相同的密钥。例如

<system.web>
  <machineKey validationKey="*********" 
              decryptionKey="*********" 
              validation="HMACSHA256" 
              decryption="AES" />
</system.web>

请有人告诉我如何在 ASP.NET Core 2.0 中完成此操作?


你需要使用数据保护 API https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/ now:

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

样品可以在官方找到数据保护存储库 https://github.com/aspnet/DataProtection.

顺便说一下,同样的方法也适用于 ASP.NET:更换<machineKey>在 ASP.NET 中 https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/compatibility/replacing-machinekey


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

注册IDataProtectionProvider进入 DI 使用.AddDataProtection方法:

public void ConfigureServices(IServiceCollection services)
{
    // Adds data protection services
    services.AddDataProtection();
    ...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 ASP.NET Core 2.0 中实现 machineKey 的相关文章

随机推荐