注意:此问题现已解决 - 请参阅下面的更新3 以获取解决方案。
我有一个ASP.NET Core 2 Web应用程序,需要连接到SQL Server数据库。根据我的 Update 2 ,我正在使用IIS调试应用程序。
我正在我的Program
类中加载配置(因为我需要它来设置日志记录),如下所示:
public static IConfiguration Configuration => new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{EnvName ?? "Production"}.json", optional: true)
.AddUserSecrets<Startup>(false)
.Build();
我的BuildWebHost
方法如下所示:
public static IWebHost BuildWebHost(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseConfiguration(Configuration)
.UseSerilog()
.Build();
}
我的appSettings.json
文件包含此部分:
{
"ConnectionStrings": {
"DefaultConnection": "*****" // secret
}
}
我已经使用Visual Studio中的上下文菜单将一个用户机密文件添加到项目中,复制了上面的部分但是使用了真实的连接字符串。
完成所有操作后,我的代码会抛出有关连接字符串格式的异常。但是,如果我用我的主appSettings.json
文件中的“*****”替换真实连接字符串,应用程序工作正常。所以我认为它没有加载我的用户机密。
现在,我认为使用AddUserSecrets
的重载传递参数false
会导致代码在无法加载用户机密时中断。但它并没有打破这里。我不确定我还能做什么。什么会导致ASP.NET Core无法加载用户机密?
更新1
调试时,我可以在Configuration属性中看到它有3个我期望的提供者:appsettings.json
,appsettings.Development.json
和secrets.json
。但是,秘密提供程序的文件根目录是我的调试路径,而不是我的秘密文件的位置,即C:\ Users [用户名] \ AppData \ Roaming \ Microsoft \ UserSecrets ...
更新2
我意识到Web项目的Debug设置指向一个IIS站点,该站点使用在ApplicationPoolIdentity
用户下运行的应用程序池。这是否意味着用户机密需要在C:\ Users [app-pool-user] \ AppData \ Roaming \ Microsoft \ UserSecrets而不是我自己的用户帐户?我已经尝试将GUID命名的secrets.json文件夹复制到此位置,但这没有帮助。但是,我尝试更改为在IIS Express下运行,这次用户机密 已加载。但由于各种原因,我需要能够在特定域名下调试此应用程序,那么如何在我的IIS上下文中加载我的用户机密?我尝试更改应用程序池以使用我的主Windows用户而不是AppPoolIdentity
,但这没有帮助。
更新3:已解决
好吧,我今天学到了一些东西!最终它the answer here解决了我的问题,但不是我想象的那样。我转移了我的原始问题 - 加载用户机密 - 因为我通过在IIS上托管实现我基本上使用部署而不是临时调试会话。因此,我将用户机密信息移至环境变量(例如,在我的连接字符串示例中,添加系统环境变量ConnectionStrings:DefaultConnection
)并在我的配置设置中添加AddEnvironmentVariables()
。但我仍然发现由于某些原因这些没有被加载到我的配置中。最后,我发现感谢this SO post IIS有一个地方可以添加隐藏在名为Configuration Editor的东西深处的本地环境变量。在这里添加我的变量解决了这个问题,这意味着我现在可以在IIS中本地托管和调试,同时保证我的秘密安全。
答案 0 :(得分:4)
我发现在IIS下运行时,secretts.json应该位于网站的物理路径中。
答案 1 :(得分:0)
大多数文档假定您使用的是IIS Express,并且没有涵盖开发计算机上本地IIS实例的情况。
鉴于IIS应用程序池以有限的特权运行(例如,想象如果默认情况下它可以读取[user] \ desktop \ ExpenseReport.xls,将会发生什么情况)。因此,期望将secrets.json位于网站文件夹中的某个位置。只要确保将其添加到.gitignore文件(或替代文件)中,它仍然是“秘密”。
答案 2 :(得分:-1)
简短的回答-在IIS下,工作进程将具有其自己的“%APPDATA%”环境变量。 在我的桌面上,它是“ C:\ WINDOWS \ system32 \ config \ systemprofile \ AppData \ Roaming” 如果我然后将带有我的秘密的目录复制到此处并授予适当的asp.net辅助进程(例如“ IIS AppPool \ AppPoolName”-替换您的应用程序池名称)对该目录的读取访问权限,则该目录应该起作用。 更长的答案
感谢开源的奇迹,您可以在此处找到代码-https://github.com/aspnet/Extensions/blob/master/src/Configuration/Config.UserSecrets/src/UserSecretsConfigurationExtensions.cs
...
return AddSecretsFile(configuration, PathHelper.GetSecretsPathFromSecretsId(userSecretsId), reloadOnChange);
}
private static IConfigurationBuilder AddSecretsFile(IConfigurationBuilder configuration, string secretPath, bool reloadOnChange)
{
var directoryPath = Path.GetDirectoryName(secretPath);
var fileProvider = Directory.Exists(directoryPath)
? new PhysicalFileProvider(directoryPath)
: null;
return configuration.AddJsonFile(fileProvider, PathHelper.SecretsFileName, optional: true, reloadOnChange);
}
当您查看此内容时,可以看到为什么@kraihn的答案起作用-如果该目录不存在(或者由于权限而无法读取-导致我暂时离开了目录),则将fileProvider设置为null-就像其他文件一样,它在应用程序目录中查找。
目录本身是在“ PathHelper”(https://github.com/aspnet/Extensions/blob/master/src/Configuration/Config.UserSecrets/src/PathHelper.cs)中设置的-但只要有“ APPDATA”设置,它将自动进行选择。
...
const string userSecretsFallbackDir = "DOTNET_USER_SECRETS_FALLBACK_DIR";
// For backwards compat, this checks env vars first before using Env.GetFolderPath
var appData = Environment.GetEnvironmentVariable("APPDATA");
var root = 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.");
}
return !string.IsNullOrEmpty(appData)
? Path.Combine(root, "Microsoft", "UserSecrets", userSecretsId, SecretsFileName)
: Path.Combine(root, ".microsoft", "usersecrets", userSecretsId, SecretsFileName);
}
您可以使用sysinternals进行验证 检查目录是否存在-
15:04:16.0759461 w3wp.exe 26308 CreateFile C:\Windows\System32\config\systemprofile\AppData\Roaming\Microsoft\UserSecrets\c89b3c54-eda4-4b3b-97ca-b7d3d3699622 SUCCESS Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a, OpenResult: Opened
15:04:16.0759965 w3wp.exe 26308 QueryNetworkOpenInformationFile C:\Windows\System32\config\systemprofile\AppData\Roaming\Microsoft\UserSecrets\c89b3c54-eda4-4b3b-97ca-b7d3d3699622 SUCCESS CreationTime: 27/12/2019 14:31:05, LastAccessTime: 27/12/2019 14:31:05, LastWriteTime: 27/12/2019 14:31:05, ChangeTime: 27/12/2019 15:03:45, AllocationSize: 01/01/1601 00:00:00, EndOfFile: 01/01/1601 00:00:00, FileAttributes: D
15:04:16.0760114 w3wp.exe 26308 CloseFile C:\Windows\System32\config\systemprofile\AppData\Roaming\Microsoft\UserSecrets\c89b3c54-eda4-4b3b-97ca-b7d3d3699622 SUCCESS
15:04:16.0762556 w3wp.exe 26308 CreateFile C:\Windows\System32\config\systemprofile\AppData\Roaming\Microsoft\UserSecrets\c89b3c54-eda4-4b3b-97ca-b7d3d3699622 SUCCESS Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a, OpenResult: Opened
15:04:16.0762940 w3wp.exe 26308 QueryNetworkOpenInformationFile C:\Windows\System32\config\systemprofile\AppData\Roaming\Microsoft\UserSecrets\c89b3c54-eda4-4b3b-97ca-b7d3d3699622 SUCCESS CreationTime: 27/12/2019 14:31:05, LastAccessTime: 27/12/2019 14:31:05, LastWriteTime: 27/12/2019 14:31:05, ChangeTime: 27/12/2019 15:03:45, AllocationSize: 01/01/1601 00:00:00, EndOfFile: 01/01/1601 00:00:00, FileAttributes: D
15:04:16.0763067 w3wp.exe 26308 CloseFile C:\Windows\System32\config\systemprofile\AppData\Roaming\Microsoft\UserSecrets\c89b3c54-eda4-4b3b-97ca-b7d3d3699622 SUCCESS
不从IIS目录中读取配置文件的机密文件。
15:04:16.0759461 w3wp.exe 26308 CreateFile C:\Windows\System32\config\systemprofile\AppData\Roaming\Microsoft\UserSecrets\c89b3c54-eda4-4b3b-97ca-b7d3d3699622 SUCCESS Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a, OpenResult: Opened
15:04:16.0759965 w3wp.exe 26308 QueryNetworkOpenInformationFile C:\Windows\System32\config\systemprofile\AppData\Roaming\Microsoft\UserSecrets\c89b3c54-eda4-4b3b-97ca-b7d3d3699622 SUCCESS CreationTime: 27/12/2019 14:31:05, LastAccessTime: 27/12/2019 14:31:05, LastWriteTime: 27/12/2019 14:31:05, ChangeTime: 27/12/2019 15:03:45, AllocationSize: 01/01/1601 00:00:00, EndOfFile: 01/01/1601 00:00:00, FileAttributes: D
15:04:16.0760114 w3wp.exe 26308 CloseFile C:\Windows\System32\config\systemprofile\AppData\Roaming\Microsoft\UserSecrets\c89b3c54-eda4-4b3b-97ca-b7d3d3699622 SUCCESS
15:04:16.0762556 w3wp.exe 26308 CreateFile C:\Windows\System32\config\systemprofile\AppData\Roaming\Microsoft\UserSecrets\c89b3c54-eda4-4b3b-97ca-b7d3d3699622 SUCCESS Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a, OpenResult: Opened
15:04:16.0762940 w3wp.exe 26308 QueryNetworkOpenInformationFile C:\Windows\System32\config\systemprofile\AppData\Roaming\Microsoft\UserSecrets\c89b3c54-eda4-4b3b-97ca-b7d3d3699622 SUCCESS CreationTime: 27/12/2019 14:31:05, LastAccessTime: 27/12/2019 14:31:05, LastWriteTime: 27/12/2019 14:31:05, ChangeTime: 27/12/2019 15:03:45, AllocationSize: 01/01/1601 00:00:00, EndOfFile: 01/01/1601 00:00:00, FileAttributes: D
15:04:16.0763067 w3wp.exe 26308 CloseFile C:\Windows\System32\config\systemprofile\AppData\Roaming\Microsoft\UserSecrets\c89b3c54-eda4-4b3b-97ca-b7d3d3699622 SUCCESS