簡體   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