UserSecrets: Add fallbacks when HOME or APPDATA env vars are unset (#873)
This commit is contained in:
Родитель
804b12792e
Коммит
79e15706cf
|
@ -40,8 +40,19 @@ namespace Microsoft.Extensions.Configuration.UserSecrets
|
|||
badCharIndex));
|
||||
}
|
||||
|
||||
var root = Environment.GetEnvironmentVariable("APPDATA") ?? // On Windows it goes to %APPDATA%\Microsoft\UserSecrets\
|
||||
Environment.GetEnvironmentVariable("HOME"); // On Mac/Linux it goes to ~/.microsoft/usersecrets/
|
||||
const string userSecretsFallbackDir = "DOTNET_USER_SECRETS_FALLBACK_DIR";
|
||||
|
||||
// For backwards compat, this checks env vars first before using Env.GetFolderPath
|
||||
var root = Environment.GetEnvironmentVariable("APPDATA") // On Windows it goes to %APPDATA%\Microsoft\UserSecrets\
|
||||
?? Environment.GetEnvironmentVariable("HOME") // On Mac/Linux it goes to ~/.microsoft/usersecrets/
|
||||
?? Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
|
||||
?? Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
|
||||
?? Environment.GetEnvironmentVariable(userSecretsFallbackDir); // this fallback is an escape hatch if everything else fails
|
||||
|
||||
if (string.IsNullOrEmpty(root))
|
||||
{
|
||||
throw new InvalidOperationException("Could not determine an appropriate location for storing user secrets. Set the " + userSecretsFallbackDir + " environment variable to a folder where user secrets should be stored.");
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("APPDATA")))
|
||||
{
|
||||
|
|
Загрузка…
Ссылка в новой задаче