![](/img/trans.png)
[英]Asp.net core 2.0 Configure.Services<model>() binding isn't working with user secrets
[英]ASP.NET Core 2 web application isn't loading user secrets when debugging IIS website
注意:此问题现已解决 - 请参阅下面的更新 3以获取解决方案。
我有一个需要连接到 SQL Server 数据库的 ASP.NET Core 2 Web 应用程序。 根据下面的更新 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
调试时我可以在我的配置属性里看到它有3个提供商,我期望: appsettings.json
, appsettings.Development.json
和secrets.json
。 但是,机密提供程序的文件根目录是我的调试路径,而不是我的机密文件的位置,即 C:\\Users[username]\\AppData\\Roaming\\Microsoft\\UserSecrets...
更新 2
我已经意识到 web 项目的调试设置指向一个 IIS 站点,该站点使用在ApplicationPoolIdentity
用户下运行的ApplicationPoolIdentity
程序池。 这是否意味着用户机密需要位于 C:\\Users[app-pool-user]\\AppData\\Roaming\\Microsoft\\UserSecrets 而不是我自己的用户帐户? 我已经尝试将 GUID 命名的 secrets.json 文件夹复制到这个位置,但这并没有帮助。 我有,但是,试图改变下IIS Express和这一次的用户秘密被加载运行。 但是出于各种原因,我需要能够在特定域名下调试此应用程序,因此如何将我的用户机密加载到我的 IIS 上下文中? 我尝试更改应用程序池以使用我的主要 Windows 用户而不是AppPoolIdentity
但这没有帮助。
更新 3:已解决
嗯,我今天学到了一些东西! 最终是这里的答案解决了我的问题,但不是以我预期的方式。 我从最初的问题——用户机密的加载——继续前进,因为我意识到通过托管在 IIS 上,我实际上是在处理部署而不是临时调试会话。 所以我将我的用户机密移到环境变量中(例如,在我的连接字符串示例中,添加系统环境变量ConnectionStrings:DefaultConnection
)并将AddEnvironmentVariables()
添加到我的配置设置中。 但是我仍然发现由于某种原因这些没有被加载到我的配置中。 最后,由于这篇 SO 帖子,我发现 IIS 有一个地方可以添加隐藏在名为配置编辑器的东西深处的本地环境变量。 在这里添加我的变量解决了这个问题,这意味着我现在可以在 IIS 中本地托管和调试,同时保证我的机密安全。
我发现在 IIS 下运行时,secrets.json 应该在站点的物理路径中。
大多数文档都假设您使用的是 IIS Express,并且没有涵盖在开发机器上使用本地 IIS 实例的场景。
鉴于 IIS 应用程序池以有限的权限运行(例如,想象一下如果默认情况下它可以读取 [user]\\desktop\\ExpenseReport.xls 会发生什么)。 因此,期望secrets.json 位于网站文件夹中的某个位置。 只要确保你将它添加到你的 .gitignore 文件(或替代文件)中,它仍然是一个“秘密”。
**简单的方法-已针对.Net Core 2.2进行了测试,并且完美运行**
在IIS服务器上发布时,无法访问用户机密,因此我在appsettings.json文件中添加了机密属性,如下所示。 之后,可以使用以下命令访问
之后,在startup.cs类的行下面添加
services.AddTransient<IEmailSender, EmailSender>();
services.Configure<AuthMessageSenderOptions>(Configuration.GetSection("UserSecrets"));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddAuthentication()
.AddGoogle(options =>
{
//IConfigurationSection googleAuthNSection =
// Configuration.GetSection("Authentication:Google");
//options.ClientId = googleAuthNSection["ClientId"];
//options.ClientSecret = googleAuthNSection["ClientSecret"];
options.ClientId = Configuration["UserSecrets:Authentication:Google:ClientId"];
options.ClientSecret = Configuration["UserSecrets:Authentication:Google:ClientSecret"];
})
.AddFacebook(facebookOptions =>
{
facebookOptions.AppId = Configuration["UserSecrets:Authentication:Facebook:AppId"];
facebookOptions.AppSecret = Configuration["UserSecrets:Authentication:Facebook:AppSecret"];
})
.AddTwitter(twitterOptions =>
{
twitterOptions.ConsumerKey = Configuration["UserSecrets:Authentication:Twitter:ConsumerAPIKey"];
twitterOptions.ConsumerSecret = Configuration["UserSecrets:Authentication:Twitter:ConsumerSecret"];
})
.AddMicrosoftAccount(microsoftOptions =>
{
microsoftOptions.ClientId = Configuration["UserSecrets:Authentication:Microsoft:ClientId"];
microsoftOptions.ClientSecret = Configuration["UserSecrets:Authentication:Microsoft:ClientSecret"];
});
services.AddSingleton<CountryService>();
// string here = Configuration["UserSecrets:Twilio:AccountSID"];
var accountSid = Configuration["UserSecrets:Twilio:AccountSID"];
var authToken = Configuration["UserSecrets:Twilio:AuthToken"];
TwilioClient.Init(accountSid, authToken);
services.Configure<TwilioVerifySettings>(Configuration.GetSection("UserSecrets:Twilio"));
它针对.Net Core 2.2进行了测试和工作
简短回答 - 在 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
当您查看此内容时,您会明白为什么 @kraihn 的答案有效 - 如果目录不存在(或由于权限而无法读取 - 这让我感到困惑了一段时间)然后“fileProvider”设置为 null - 所以它看起来在应用程序目录中 - 就像其他配置文件一样。
目录本身设置在“PathHelper”( https://github.com/aspnet/Extensions/blob/master/src/Configuration/Config.UserSecrets/src/PathHelper.cs )中 - 但只要有“ APPDATA”设置它会选择它。
您可以使用 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.