【问题标题】:How does one distribute Data Protection keys with a .NET Core web app?如何使用 .NET Core Web 应用分发数据保护密钥?
【发布时间】:2019-01-30 07:40:41
【问题描述】:

我有点不清楚数据保护密钥在网络场环境中的工作方式。我没有所有服务器都可以使用的公共位置(并且不想处理权限)。因此,我想生成一个密钥并将其与 Web 应用程序一起分发。

我正在执行以下操作,但不确定是否正确。所以我在我的开发电脑上本地生成了一个密钥文件:

var specialFolder = Environment.SpecialFolder.CommonApplicationData;
var appDataPath = Path.Combine(
     Environment.GetFolderPath(specialFolder),
    "company", 
    "product"
);
services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(appDataPath));

这将创建一个key-some-guid.xml 文件。然后,我将这个文件与我的 Web 应用程序一起分发。

现在,当我运行我的 Web 应用程序时,在 Startup.Configure 服务中,将此文件复制到 appDataPath 位置(如上定义)并调用 services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(appDataPath));

这行得通吗?还是我从根本上错过了什么?

【问题讨论】:

  • Will that work?你尝试的时候发生了什么?
  • .Net Core 应该支持通过 Microsoft.AspNetCore.DataProtection 保护数据。
  • @mjwills 它似乎按照下面的答案工作。但它似乎非常笨拙。感觉应该有更好的解决方案。绝对是矫枉过正,只是为了使用 TempData。
  • Just 是一个有趣的词。您想使用TempData。这意味着您要共享密钥。没有神奇的方法可以解决这个问题。这意味着您需要DataProtection。如果您对此有解决方案(听起来像您有),那么您应该使用它。

标签: c# .net asp.net-core .net-core


【解决方案1】:

回答我自己的问题。以下似乎适用于网络场。从 Startup.ConfigureServices 调用以下方法。它假定密钥(在开发机器上生成)位于项目根目录下的 Keys 文件夹中。

public Startup(IHostingEnvironment env)
{
    /* skipping boilerplate setup code */

    Environment = env;
}

private IHostingEnvironment Environment { get; set; }

private void ConfigureDataProtection(IServiceCollection services) {
    // get the file from the Keys directory
    string keysFile = string.Empty;
    string keysPath = Path.Combine(Environment.ContentRootPath, "Keys");
    if (!Directory.Exists(keysPath)) {
        Log.Add($"Keys directory {keysPath} doesn't exist");
        return;
    }

    string[] files = Directory.GetFiles(keysPath);
    if (files.Length == 0) {
        LLog.Add($"No keys found in directory {keysPath}");
        return;
    } else {
        keysFile = files[0];

        if (files.Length >= 2) {
            LLog.Add($"Warning: More than 1 key found in directory {keysPath}.  Using first one.");
        }
    }

    // find and optionally create the path for the key storage
    var appDataPath = Path.Combine(
        System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonApplicationData),
        "companyName",
        "productName"
    );

    if (!Directory.Exists(appDataPath)) {
        try {
            Directory.CreateDirectory(appDataPath);
        } catch (Exception ex) {
            Log.Add($"Error creating key storage folder at {appDataPath}.  Error: {ex.Message}");
            return;
        }
    }

    // delete any keys from the storage directory
    try {
        foreach (var file in new DirectoryInfo(appDataPath).GetFiles()) file.Delete();
    } catch (Exception ex) {
        Log.Add($"Error deleting keys from {appDataPath}.  Error: {ex.Message}");
        return;
    }

    try {
        string targetPath = Path.Combine(appDataPath, new FileInfo(keysFile).Name);
        File.Copy(keysFile, targetPath, true);
    } catch (Exception ex) {
        Log.Add($"Error copying key file {keysFile} to {appDataPath}.  Error: {ex.Message}");
        return;
    }

    // everything is in place
    services.AddDataProtection()
        .PersistKeysToFileSystem(new DirectoryInfo(appDataPath))
        .DisableAutomaticKeyGeneration();
}

【讨论】:

    猜你喜欢
    • 2019-02-04
    • 1970-01-01
    • 2011-06-11
    • 2010-09-19
    • 2020-12-12
    • 2011-06-23
    • 2017-02-17
    • 2018-01-29
    • 1970-01-01
    相关资源
    最近更新 更多