c# - 如何使用 .NET Core Web 应用程序分发数据保护 key ?

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

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

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

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 应用程序一起分发。

现在,当我运行我的网络应用程序时,在 Startup.Configure 服务中,将此文件复制到 appDataPath 位置(如上定义)并调用 services.AddDataProtection().PersistKeysToFileSystem(新目录信息(应用程序数据路径));

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

最佳答案

回答我自己的问题。以下内容似乎适用于整个网络场。从 Startup.ConfigureServices 调用下面的方法。它假定 key (在开发机器上生成)位于项目根目录下的 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();
}

关于c# - 如何使用 .NET Core Web 应用程序分发数据保护 key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54429757/

相关文章:

c# - 在没有页面的情况下使用 LoadControl

.net - 为 ASP.NET API 微服务实现 API 网关

.net - 如何多次使用 ConsoleCancelEventHandler

c# - 如何从 Utf8JsonReader 获取属性路径?

c# - Process.Close() 没有终止创建的进程,c#

c# - 如何从 wav 文件中检索帧编号和帧大小

c# - 如何在C#中的同一线程上监听standard in和套接字?

.net - 在运行时更改 .NET 配置而不修改 app.config

asp.net-core - Twilio Messaging Webhook - 不支持的媒体类型 - Asp.net Core 3.1 API

asp.net-core - User.Claims.FirstOrDefault().Value 在登录操作中为空(ASP.NET Identity 3)?