File Path for PersistKeysToFileSystem on shared server

南楼画角 提交于 2020-01-04 04:08:07

问题


I'm trying to make my keys persist for users that log in. As I'm currently using shared hosting for the website, I've decided to use the file system to store the keyring. So the code looks like this:

services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(""))
.SetApplicationName("MyWebsite")
.SetDefaultKeyLifetime(TimeSpan.FromDays(90))
.ProtectKeysWithCertificate(cert);

However, what I'm not really understanding is where I should hold these keys, and what would be the path I pass in in order for them to be there. Since this is an MVC Core Application I am a little confused, in an MVC 5 I would put it in App_Data folder, but here there is no App_Data folder and I want to make sure it stays secure and cannot be accessed via the browser.

The other thing is do I pass it a relative path or a direct path? If it is relative, where is my starting point? Is it bin, root directory or something else?


回答1:


The simplest way is probably to create a folder inside the app folder. For example, create a folder called Keys, and use the IHostingEnvironment object to get the app folder. Something like this:

public class Startup
{
    private readonly IHostingEnvironment _environment;

    public Startup(IHostingEnvironment environment)
    {
        _environment = environment;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        var keysFolder = Path.Combine(_environment.ContentRootPath, "Keys");

        services.AddDataProtection()
            .PersistKeysToFileSystem(new DirectoryInfo(keysFolder))
            .SetApplicationName("MyWebsite")
            .SetDefaultKeyLifetime(TimeSpan.FromDays(90))
            .ProtectKeysWithCertificate(cert);
    }

    // snip
}


来源:https://stackoverflow.com/questions/55314845/file-path-for-persistkeystofilesystem-on-shared-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!