如何在 ASP.NET Core 2.0 中实现 machineKey [英] How to implement machineKey in ASP.NET Core 2.0

查看:21
本文介绍了如何在 ASP.NET Core 2.0 中实现 machineKey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In ASP.NET (not core) I would normally add a machineKey to the web.config so that I could perform some functions on a local machine instead of the server so that database/callback operations would use the same key. Eg

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

请有人建议如何在 ASP.NET Core 2.0 中做到这一点?

Please can someone advise how this can be done in ASP.NET Core 2.0?

推荐答案

您需要使用 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<表示)/code> 接口)通过 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();
    ...
}

这篇关于如何在 ASP.NET Core 2.0 中实现 machineKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆