【问题标题】:Using an in-memory repository. Keys will not be persisted to storage After published to IIS使用内存存储库。发布到 IIS 后,密钥将不会保存到存储中
【发布时间】:2020-10-20 09:32:02
【问题描述】:

当我在 VS 2019 上运行我的 asp.net 3.1 应用程序时,它运行良好,没有问题。在我发布到本地 IIS 后,我遇到了这个问题(使用内存存储库。密钥不会被持久化到存储中。用户配置文件和 HKLM 注册表都不可用。使用临时密钥存储库。当应用程序退出时,受保护的数据将不可用.)。我也尝试在 IIS 高级设置上设置加载用户配置文件。我试过这篇文章Neither user profile nor HKLM registry available. Using an ephemeral key repository. Protected data will be unavailable when application exits。但是我的问题没有解决。 任何建议或指导将不胜感激。

谢谢艾米

【问题讨论】:

标签: c# asp.net


【解决方案1】:

我在日志中也遇到了这些错误消息,但在我们的例子中,这是因为我们在 Linux 上运行 ASP.NET Core,而 Linux 上既没有 IIS 也没有 HKLM 注册表。

主要症状是当我们部署新版本的服务器时,用户会遇到身份验证错误。这是因为如错误消息所述,密钥存储在内存中,并且在重新部署应用程序时,密钥会丢失,并且在启动新版本的应用程序时会重新生成新的密钥。

解决方案是将密钥保存到本地存储中。在您的 startup.cs 文件中,尝试添加以下行:

using Microsoft.AspNetCore.DataProtection;

public void ConfigureServices(IServiceCollection services) {
    // other config code

    services.AddDataProtection()
        .PersistKeysToFileSystem(new DirectoryInfo(@"C:\someFolder\"));
    // config code for authentication
    // other config code
}

由于我在 Windows 机器上进行本地测试,“someFolder”实际上是从 appsettings.json 和 appsettings.Development.json 读取的配置值。 Linux 上的路径不同,运行 .NET 应用程序的进程需要拥有并拥有进程的读写权限(ubuntu 上的 www-data 和 nginx)

请注意,将密钥保存到文件系统会自动删除加密,但可以重新添加加密。

有关这些的 MS 文档:
https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/implementation/key-storage-providers?view=aspnetcore-5.0&tabs=visual-studio

https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/implementation/key-encryption-at-rest?view=aspnetcore-5.0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 2018-02-20
    相关资源
    最近更新 更多