我在 ASP.NET Core 2.0 应用程序中使用 Microsoft.AspNetCore.DataProtection
来保护数据。对于默认设置,我在 Startup.cs
文件中添加了以下代码
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddDataProtection().ProtectKeysWithDpapi();
...
...
}
但是该代码会引发错误:
InvalidOperationException: The 'IXmlRepository' instance could not be found. When an 'IXmlEncryptor' instance is set, a corresponding 'IXmlRepository' instance must also be set.
我在这个实现中遗漏了什么吗?
最佳答案
您指定了如何加密 key (IXmlEncryptor
的 Windows DPAPI 实现)但您没有指定保存加密 key 的位置(IXmlRepository
)。有多种选项可以保留 key ,例如文件系统、注册表或云中的某个远程位置。
尝试使用 PersistKeysToFileSystem()
或 PersistKeysToRegistry()
。建议你看看at the documentation关于 ASP.NET Core 数据保护配置。
关于c# - ASP.NET Core 中的 key 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55000472/