繁体   English   中英

如何使用.NET Core Web应用程序分发数据保护密钥?

[英]How does one distribute Data Protection keys with a .NET Core web app?

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

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

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));

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

回答我自己的问题。 以下内容似乎适用于整个Web场。 从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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM