我正在尝试让我的密钥对登录的用户持续存在.由于我目前正在为网站使用共享主机,因此我决定使用文件系统来存储密钥环.所以代码看起来像这样:
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);
但是,我不太了解我应该将这些钥匙放在哪里,以及我要经过什么路径才能让它们在那里.由于这是一个 MVC 核心应用程序,我有点困惑,在 MVC 5 中我会将它放在 App_Data 文件夹中,但这里没有 App_Data 文件夹,我想确保它保持安全并且无法通过浏览器访问.
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.
另一件事是我传递的是相对路径还是直接路径?如果是相对的,我的出发点在哪里?是bin、根目录还是别的什么?
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?
最简单的方法大概是在app文件夹里面创建一个文件夹.例如,创建一个名为 Keys
的文件夹,并使用 IHostingEnvironment
对象获取 app 文件夹.像这样的:
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
}
这篇关于共享服务器上 PersistKeysToFileSystem 的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!